How do I create a JSON string in C # / NETMF?

I am currently using a string value MyFinalJSON

to create a string.

as MyFinalJSON += "\"Name\":\"" + myNameString +"\"";

This seems like a very long winding / fallacious way to write in JSON format.

Is there a better / more appropriate way to create JSON strings? Especially when the JSON string can be very long.

EDIT  Sorry, I forgot to mention that I am using the NetMF framework and therefore there is no namespace for system.web...

, among many others

Any advice is greatly appreciated.

+3


source to share


3 answers


Use json.netmf or download and extract file from here

Github: https://github.com/mweimer/Json.NetMF



From the documentation, here's the use of.

string json = JsonSerializer.SerializeObject(o, DateTimeFormat.Default);

      

+1


source


You are correct to ask this as this is indeed the longest way to achieve this (although you could probably google'd it and find your solution within minutes).

However, he named serialization:



using System.Web.Script.Serialization;

var myNameString = "DeeMac";
var myObject = { Name: myNameString };
var json = new JavaScriptSerializer().Serialize(myObject);

      

NOTE. There are several different serialization libraries.

+1


source


try using JavascriptSerializer or NewtonSoft library ... for JavascriptSerializerClass DeeMac answer looks good :)

For the sample library NewtonSoft. According to the documentation on this link:

Product product = new Product();
product.ExpiryDate = new DateTime(2008, 12, 28);

JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;

using (StreamWriter sw = new StreamWriter(@"c:\json.txt"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
    serializer.Serialize(writer, product);
    // {"ExpiryDate":new Date(1230375600000),"Price":0}
}

      

-3


source







All Articles