ラベル json の投稿を表示しています。 すべての投稿を表示
ラベル json の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

Retrieve JSON result set

What is the best method for retrieving a result set and returning a JSON object? I tried using a DataSet, but received a serialization error.

TIA

You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer.


You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer. The DataSet is not supported with the Current Version, Add the January CTP to work with DataSet and DataTable.


I am a little confused. Can you provide an example of how to retrieve a JSON serialized result set? Where do I find the January CTP?

TIA


You can find the January CTP from this linkhttp://www.microsoft.com/downloads/details.aspx?familyid=4CB52EA3-9548-4064-8137-09B96AF97617&displaylang=en

2012年3月24日土曜日

Return json from xml document

I am trying to return a json object from an XML docuement, but the returned object is not in json format. What am i misisng?

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
PublicFunction GetFormObject( _
ByVal strFileLocAsString)As XmlDocument
Try
Dim xmlDocAsNew XmlDocument
xmlDoc.Load(strFileLoc)
Return xmlDoc
Catch allExceptionsAs Exception
MsgBox(HttpUtility.HtmlEncode(allExceptions.Message))
ReturnNothing
EndTry
EndFunction

TIA

Is the XmlDocument class marked as serializable? It needs to be in order to be remoted by JSON.


Hi Scott,

How does one mask an xml document as serializable?

Ron Tornambe


Hi,

I am not sure, but here may be the answer:http://forums.asp.net/p/1054378/1496599.aspx

I am not sure, did you try to specify content-type in request?

Content-Type: application/json

Here the request generated by AJAX.NET Beta 2 proxy, see 'js' before method name:

POST /Source/service.asmx/js/UpdateChat HTTP/1.1
Host: localhost:1123
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Content-Type: application/json
Content-Length: 50
Cookie: ASP.NET_SessionId=qpspnn45mgxzlp20xdj0dvng; .SLCVISITOR=200529225
Pragma: no-cache
Cache-Control: no-cache

{"a":246144119,"v":200529225,"n":18,"z":-1,"m":[]}

Thankfomine for providing the answer.

Best Regards


You can use theNewtonsoft.Json libary。

like this:

[WebMethod]
[ScriptMethod]
public string GetData()
{
XmlDataDocument doc =new System.Xml.XmlDataDocument();
doc.LoadXml("/test.xml");

using (StringWriter sw =new StringWriter())
{
using (JsonWriter jsonWriter =new JsonWriter(sw))
{
jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;

JsonSerializer jsonSerializer =new JsonSerializer();
jsonSerializer.Converters.Add(new XmlNodeConverter());

jsonSerializer.Serialize(jsonWriter, doc);
}
return sw.ToString();
}

}

Returning dynamic JSON from ASP.Net

I have an ASP.Net web service that I want to return a JSON response. The method that I am working on in particular calls another web service that already gives me a JSON response. All I want to do is return that to the caller of my webservice (it will be a .htm/.js caller).

I have already found a way to make it work, but it doesn't seem optimal. If I define some local objects in my web service that look like the JSON response I get from the third party web service, I can deserialize the response into my local objects, and return the object from my web service. If I call the service from the browser with the JSON request type, I get back the correct JSON response. But in this case I have taken a JSON string, converted it to an object and back again, which seems like too much work. Plus, I'll have an extra layer of objects that have to be kept in sync with the third party response.

If I just return the JSON that I get from the third party webservice (as a string), in the browser I get a string that can't be EVAL'd into a js object. It has quotes around the whole thing and all the internal quotes are escaped, etc. It is basically just a string, not JSON.

So, the question is, how can I define a web service method that returns any JSON that can be called from a simple htm/js client and get it to return valid (ready to eval) JSON? What return type should I be using?

Backslider

ASP.NET AJAX web services only support returning objects. These objects will be serialized by the AJAX services layer. There is no returntype to hold raw JSON , and skip this serialization.

However if you return the JSON as a string, you should be able to eval the response once to get the string response from thirdparty webservice(If you use a proxy generated by ASP.NET AJAX it would returned an already eval'ed response). Once you get the string, you should be able to eval it again to get the actual js object.


Have you tried letting your server convert it into a .Net object?