c# - Json.NET parse array in object -


i'm having extremely difficult time parsing multiple arrays out of array, embedded in several object in json.

basically json looks like

{  took:8,  success:true,  items:[   keywords:{...},   maininfo:{     name:'...',     expdate:'...',     targetcities:[...],     targetstates:[...]   },   additionalinfo:{     skills:[],     hometime:''   } } 

my c# looks like:

public class job{   public string name{get;set;}   public list<string> targetcities{get;set;}   public list<string> targetstates{get;set;}   public list<string> skills{get;set;}   public string hometime{get;set;} } public class jobs{   private jobject o;   private list<job> jobs;   public jobs(string json){     this.o=jobject.parse(json);   }   public list<job> tolist(){     list<jobject> alljobs=o["items"].select(t => (jobject)t).tolist();     foreach(jobject in alljobs){       job j=new job();       j.name=(string)i["maininfo"]["name"];       j.targetcities=i["maininfo"]["targetcities"].select(t =>(string)t).tolist();       j.targetstates=i["maininfo"]["targetstates"].select(t =>(string)t).tolist();       j.expdate=(string)i["maininfo"]["expdate"]       j.skills=i["additionalinfo"]["skills"].select(t =>(string)t).tolist();       j.hometime=(string)i["additionalinfo"]["hometime"];       this.jobs.add(j); //error     }     return this.jobs; } 

the error null reference exception, info object reference not set instance of object., seems error jumps around unpredictably try , change code fix bug.

i no means c# or .net expert. had dealt language in past, not it. forgive me stupid error may have made.

addition:

i'm stepping through items , trying create job object each item using data accordingly.

you forgot initialize jobs field, private list<job> jobs; don't need private field if alway generate jobs list. use local variable.

public list<job> tolist()         {             jobs = new list<job>(); //add line             list<jobject> alljobs = o["items"].select(t => (jobject)t).tolist();             foreach (jobject in alljobs)             {                 job j = new job();                 j.name = (string)i["maininfo"]["name"];                 j.targetcities = i["maininfo"]["targetcities"].select(t => (string)t).tolist();                 j.targetstates = i["maininfo"]["targetstates"].select(t => (string)t).tolist();                 j.expdate = (string)i["maininfo"]["expdate"];                 j.skills = i["additionalinfo"]["skills"].select(t => (string)t).tolist();                 j.hometime = (string)i["additionalinfo"]["hometime"];                 this.jobs.add(j);              }             return this.jobs;         } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -