51

What is the simplest C# function to parse a JSON string into a object and display it (C# XAML WPF)? (for example object with 2 arrays - arrA and arrB)

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Rella
  • 65,003
  • 109
  • 363
  • 636

5 Answers5

64

Just use the Json.NET library. It lets you parse Json format strings very easily:

JObject o = JObject.Parse(@"
{
    ""something"":""value"",
    ""jagged"":
    {
        ""someother"":""value2""
    }
}");

string something = (string)o["something"];

Documentation: Parsing JSON Object using JObject.Parse

James Newton-King
  • 48,174
  • 24
  • 109
  • 130
Philip Daubmeier
  • 14,584
  • 5
  • 41
  • 77
  • Json.NET is a professional library tested and proved to be very much more flexible and efficient. Highly recommended. – Zyo Dec 22 '14 at 19:14
  • 2
    As far as I can tell, this is the only option that allows for generic object type parsing, the other answers are bound to a certain object type. – Rafael Emshoff Jun 03 '15 at 08:19
  • Great solution. Although for me worked like this `JToken something = o["something"];` – Mr. Blond Oct 24 '15 at 14:23
  • Sometimes it seems silly to pull in an external library if you need to parse json for one thing. Using DataContractJsonSerializer is a much more practical approach here. It's like pulling in EntityFramework because your application does literally a few database queries. – The Muffin Man Dec 11 '16 at 05:23
  • Using `System.Text.Json`, we can do `JsonDocument doc = JsonDocument.Parse(JsonString);` – Ritesh Aug 22 '21 at 19:35
39
DataContractJsonSerializer serializer = 
    new DataContractJsonSerializer(typeof(YourObjectType));

YourObjectType yourObject = (YourObjectType)serializer.ReadObject(jsonStream);

You could also use the JavaScriptSerializer, but DataContractJsonSerializer is supposedly better able to handle complex types.

Oddly enough JavaScriptSerializer was once deprecated (in 3.5) and then resurrected because of ASP.NET MVC (in 3.5 SP1). That would definitely be enough to shake my confidence and lead me to use DataContractJsonSerializer since it is hard baked for WCF.

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
18

I think this is what you want:

JavaScriptSerializer JSS = new JavaScriptSerializer();
T obj = JSS.Deserialize<T>(String);
Rbacarin
  • 707
  • 6
  • 12
5

You should create a structure that represents JSON keys (in case if you exactly know it) and then you can easily deserialize JSON string into your structure. In my examle I've deserialized a response from Google Cloud Message server:

class templateResponse
{
    public String multicast_id;
    public String success;
    public String failure;
    public String canonical_ids;
    public Result[] results;

    public class Result
    {
        public String message_id;
        public String registration_id;
        public String error;
    };
}

incoming JSON was:

"\"multicast_id\":7400896764380883211,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1351777805148960%39895cf0f9fd7ecd\"}]}"

So, use

templateResponse result = new JavaScriptSerializer().Deserialize<templateResponse>(json);

and you will get deserialized result object

Subtle Fox
  • 612
  • 7
  • 7
2

I would echo the Json.NET library, which can transform the JSON response into a XML document. With the XML document, you can easily query with XPath and extract the data you need. I find this pretty useful.

hopper
  • 13,060
  • 7
  • 49
  • 53