Deserialize json object xamarin android c#

前端 未结 2 1042
庸人自扰
庸人自扰 2020-12-11 10:54

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 cr

相关标签:
2条回答
  • 2020-12-11 11:13

    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;
    
    0 讨论(0)
  • 2020-12-11 11:17

    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);
    
    0 讨论(0)
提交回复
热议问题