mercredi 22 juillet 2015

Convert Json object to List

I have this Json string :

"UserProperties": [
     {
         "Id": "3", "values": [
             { "prop": "1" }
         ]
     },
     {
         "Id": "4", "values": [
             { "prop": "1" },
             { "prop": "2" },
             { "prop": "3" }
         ]
     }
]

I Want to convert this into some sort of c# string and list value like this:

public list<int> Id { get; set; }
public list<object> values { get; set; }
public int prop { get; set; }

So that i can manipulate my values :

foreach( int i in Id)
{
  foreach( object val in values)
  {
    var str = i + '-' + val.prop;
  }
}

So far what i have done is create a class that will contain those Json string. I get this code from an quite similar approach.

  1. Create a wrapper class

    class Wrapper {
        [JsonProperty("UserPropertiees")]
        public ValueSet valueSet { get; set; }
    }
    
    class ValueSet{
        [JsonProperty("values")]
        public List<string> values {get;set;}
        public PropertySet propertySet { get; set; }
    }
    
    class PropertySet{
        [JsonProperty("property")]
        public List<string> property {get;set;}
    }
    
    



Aucun commentaire:

Enregistrer un commentaire