c# - Access JSON fields with numeric keys -


i want access address of pictures in json, field name number , in c# number not valid name variable...

my json:

{   "id":3441,   "name":"test",   "address": {     "1":"url.com\/45.jpg",     "2":"url.com\/23.jpg",     "3":"url.com\/65.jpg",     "4":"url.com\/789.jpg",   },   "count":2 } 

my code in c#: (hrmlresult json)

dynamic stuff1 = newtonsoft.json.jsonconvert.deserializeobject(htmlresult); string address= stuff1.address;        //it works string allpics = stuff1.pic;           //it works  firstpictextbox.text= stuff1.pic.1;    //compiler error secondpictextbox.text = stuff1.pic[2]; //runtime error 

what should do?! thank all...

you have create model object properties, in found json you're expecting.

then, number properties, can use jsonproperty attribute name property number, example:

class mymodel {    [jsonproperty("2")]    public string 2 {get; set;} } 

and use deserializeobject<mymodel> version

this simplified example, object have maintain hierarchy , have class 'address' property , use property type in main model.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -