I just used the XmlWriter to create some XML to send back in an HTTP response. How would you create a JSON string. I assume you would just use a stringbuilder to build the JSON string and them format your response as JSON?
相关问题
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
- StackExchange API - Deserialize Date in JSON Respo
This code snippet uses the DataContractJsonSerializer from System.Runtime.Serialization.Json in .NET 3.5.
The DataContractJSONSerializer will do everything for you with the same easy as the XMLSerializer. Its trivial to use this in a web app. If you are using WCF, you can specify its use with an attribute. The DataContractSerializer family is also very fast.
I've found that you don't need the serializer at all. If you return the object as a List. Let me use an example.
In our asmx we get the data using the variable we passed along
Then using jquery we access the service, passing along that variable.
And we get our response.
If you need complex result (embedded) create your own structure:
and then you can obtain JSON string with calling
The result will be like this:
Hope it helps!
Take a look at http://www.codeplex.com/json/ for the json-net.aspx project. Why re-invent the wheel?
Using Newtonsoft.Json makes it really easier:
Documentation: Serializing and Deserializing JSON