Deserialize json object xamarin android c#

烂漫一生 提交于 2020-07-14 07:04:10

问题


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.


回答1:


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);



回答2:


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;


来源:https://stackoverflow.com/questions/38743280/deserialize-json-object-xamarin-android-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!