2

I'm having an extremely difficult time parsing multiple arrays out of an array, embedded in several object in JSON.

Basically the 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 i 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 is a Null Reference Exception, with the info Object reference not set to an instance of an object., however it seems that this error jumps around almost unpredictably as I try and change my code to fix the bug.

I am by no means a C# or .NET expert. I had dealt with the language in the past, but I personally do not like it. So forgive me for any stupid error I may have made.

ADDITION:

I'm basically stepping through all the items and trying to create a Job object from each item using the data accordingly.

2
  • How is JObject defined? Commented Mar 10, 2014 at 0:40
  • 1
    It's a JSON.net object Commented Mar 10, 2014 at 0:48

2 Answers 2

4

You forgot to initialize your jobs field, private List<Job> jobs; You don't need this private field if you alway generate the jobs list. Use a local variable.

public List<Job> toList()
        {
            jobs = new List<Job>(); //add this line
            List<JObject> allJobs = o["items"].Select(t => (JObject)t).ToList();
            foreach (JObject i 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;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I actually noticed this a bit ago. I also pulled jobs into the scope of the toList method rather than having it as a property. The problem actually happened to be that the developer who wrote the API spelled a bunch of keys wrong in the JSON while in the API documentation they are spelled correctly.
Also the json was missing some brackets, I wanted to test and could not parse it. Anyway the error was coming from that variable.
the json provided was just simple mock-up. The json that I was dealing with was about 50x larger than the example.
1

I think that you may define allJobs first

List<JObject> allJobs = new List<JObject>();
allJobs=o["items"].Select(t => (JObject)t).ToList();

4 Comments

.ToList() creates and assigns a List object.
Have you tried my code? If you don't define the instance, there is no memory allocated.
The instance is defined by the ToList method, however.
My jobs object, however, was never initialized. I'll give you +1 for bringing the thought to the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.