How to Parse JSON Without JSON.Net Library

How to parse JSON without JSON.NET library?

You can use the classes found in the System.Json Namespace which were added in .NET 4.5. You need to add a reference to the System.Runtime.Serialization assembly

The JsonValue.Parse() Method parses JSON text and returns a JsonValue:

JsonValue value = JsonValue.Parse(@"{ ""name"":""Prince Charming"", ...");

If you pass a string with a JSON object, you should be able to cast the value to a JsonObject:

using System.Json;


JsonObject result = value as JsonObject;

Console.WriteLine("Name .... {0}", (string)result["name"]);
Console.WriteLine("Artist .. {0}", (string)result["artist"]);
Console.WriteLine("Genre ... {0}", (string)result["genre"]);
Console.WriteLine("Album ... {0}", (string)result["album"]);

The classes are quite similar to those found in the System.Xml.Linq Namespace.

How to convert object to JSON without JSON.NET library?

If you want to serialize class into Json you can try this one too:

Install JSON.NET if you haven't yet.

make sure to include using Newtonsoft.Json;

and you can try this code:

Student s = new Student();
string json = JsonConvert.SerializeObject(s);

sorry for my bad english.

Parse JSON array without json.net

According to your json it actually represents one single Mod object. That said, the json serializer should be parsing/deserializing a Mod object instead of a list of Mod objects, you should reflect that object structure in your serialisation code as follows...

var mod = new JavaScriptSerializer().Deserialize<Mod>(json);

foreach (button b in mod.buttons)
{
btnName.Add(b.name);
btnText.Add(b.text);
}

C# ASP.NET Parse JSON into Dataset without JSON.NET

I think your situation is the next, what you are trying to deserialize is not a dictionary, is an array/list, to solve this you can create a class with those two properties:

public class MyClass
{
public string Task { get; set; }
public string Staff { get; set; }
}

and later you can use it to deserialize youy JSON like this:

 List<MyClass> parsedObj = JSS.Deserialize<List<MyClass>>(rawJSON);


Related Topics



Leave a reply



Submit