c# - json newtonsoft : Deserialize Object containing a list of string -


i have following issue json :

{ "evts": { "evt": [   { "id": "123456",     "key1" : "somekey",     "categ": [       "cat1",       "cat2",       "cat3"     ]   }   ]}   } 

and c# class:

public class myclass{     public string id { get; set; }     public string key1 { get; set; }       public list<string> categ { get; set; }  }  public class esobject1 {      [jsonproperty("evt")]     public list<myclass> evt { get; set; } }  public class esobject0 {      [jsonproperty("evts")]     public esobject1 evts { get; set; } } 

}

here call deserializer :

esobject0 globalobject = jsonconvert.deserializeobject<esobject0>(json); 

but last code doesnt work, throws exception : system.argumentexception: not cast or convert system.string system.collections.generic.list1[system.string].`

instead of list<string> used string [] , string nothing seems work.

how can deserialize object correctly please.

thank you.

there doesn't seem apparent problem wit hyour code working example illustrates:

using newtonsoft.json; using system; using system.collections.generic;  public class myclass {     public string id { get; set; }     public string key1 { get; set; }      public list<string> categ { get; set; }  }  public class esobject1 {     [jsonproperty("evt")]     public list<myclass> evt { get; set; } }  public class esobject0 {     [jsonproperty("evts")]     public esobject1 evts { get; set; } }   class program {     static void main()     {         string json =          @"{             ""evts"": {                 ""evt"": [                     {                         ""id"": ""123456"",                         ""key1"": ""somekey"",                         ""categ"": [                             ""cat1"",                             ""cat2"",                             ""cat3""                         ]                     }                 ]             }         }";          esobject0 globalobject = jsonconvert.deserializeobject<esobject0>(json);         foreach (string item in globalobject.evts.evt[0].categ)         {             console.writeline(item);         }     } } 

maybe fed wrong json value deserializer doesn't 1 shown in question. way, 1 shown nyour question invalid json missing , after key1 property declaration.


update:

now have shown real json (coming http://donnees.ville.quebec.qc.ca/handler.ashx?id=69&f=json) appears there's row categ not array of strings simple string:

""categ"": ""conférence"" 

now that's pretty bad design because mixing arrays , simple properties. afraid in order deal situation need use jobjects , extract information need testing actual underlying type.

for example:

var obj = jobject.parse(json); var events = (jarray)obj["evts"]["evt"]; foreach (jobject evt in events) {     var categories = evt["categ"];     if (categories jarray)     {         // you've got list of strings can loop through them         string[] cats = ((jarray)categories)             .select(x => x.value<string>())             .toarray();     }     else     {         // you've got simple string         string cat = categories.value<string>();     } } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -