2

I have little issue with deserialize json object. My json from http url:

Screen of my downloaded JSON

I don't know how to deserialize to make dynamically creating buttons. I figure out how to create buttons with text, but I don't know how to make them with options that they have. I try to get these options in Windows Form app for test, but app will crash. Thank you for help.

2 Answers 2

3

Your classes should be something like:

public class Type
{
    public int id { get; set; }
    public string name { get; set; }
    public bool closedQuestion { get; set; }
    public bool multiAnswer {get; set;}
    public bool usesImage {get; set; }
}
public class RootObject
{
    public int id { get; set; }
    public string name { get; set; }
    public Type type { get; set; }
    public List<string> options { get; set; }
}

Then you should be able to deserialize your json, using Newtonsoft.Json:

List<RootObject> myData = JsonConvert.DeserializeObject<List<RootObject>>(json);
Sign up to request clarification or add additional context in comments.

Comments

3

Using Newtonsoft.NET:

var obj = JsonConvert.DeserializeObject(json);

You can also make a pairing class and use generics:

public JsonClass {
    // Do this for each property you want to map.
    [JsonProperty(PropertyName="id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName="name")]
    public int Name { get; set; }

    [JsonProperty(PropertyName="type")]
    public MessageType Message { get; set; }
}

public class MessageType {
    [JsonProperty(PropertyName="id")]
    public int Id { get; set; }
    // etc...
}

then do:

JsonClass obj = JsonConvert.DeserializeObject<JsonClass>(json);
MessageType messageType = obj.Message;

7 Comments

Awesome! So using the example classes I wrote, put the correct [JsonProperty(PropertyName="XXX")] attribute above each property. XXX is replaced by the JSON name of the value. So, for this JSON string: { name: "Nick", age: "21" }, we would have this: [JsonProperty(PropertyName="name")] above public string Name { get; set; }.
Useful tip: Use json2csharp.com to get C# classes and properties from a valid JSON string. Just copy/paste and you will be given the code in order to create the classes for JsonConvert to Deserialize to
@NickBull no problem :) Working with a lot of APIs and web services certainly has its uses! There's also similar one for XML, too - in case future you needs to deserialize XML to C# objects: xmltocsharp.azurewebsites.net
And just to completely blow it all out of the water - Visual Studio (2010 and up) has the ability to do it natively! (I only just found this out). Click Edit > Paste Special > Paste JSON (or XML) As Classes (providing you're in a .cs file). HOW DID I NOT KNOW THIS BEFORE NOW?! See this link: visuallylocated.com/post/2015/10/05/…
@GeoffJames You are my serialization hero <3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.