2

I have json that is posted from a HTTP req. I am trying to deserialize it for use in a for each loop. Unfortunately, its format is kicking my ass as its a list of objects (i believe).

so far i have the following:

dynamic jsonObj = JsonConvert.DeserializeObject(await req.ReadAsStringAsync());

foreach (var p in jsonObj.hireSchedules)
    {
     ///do something
    }

My json is as below:

{
"hireSchedules": [
    {
        "plant": "7246054",
        "num" : "79",
        "hire": "1137277"
     },
    {
        "plant": "7246055",
        "num" : "80",
        "hire": "1137278"
     }
    ]
}

I have the following classes:

public class HireSchedule
{
    public string plant { get; set; }
    public string num { get; set; }
    public string hire { get; set; }
}

public class RootObject
{
    public List<HireSchedule> hireSchedules { get; set; }
}

Any help would be appreciated. Thanks!

7
  • RootObject jsonObj = JsonConvert.DeserializeObject<RootObject>(await req.ReadAsStringAsync()); ? Commented Oct 2, 2019 at 6:41
  • i get the following error when utilizing that before my foreach loop: "foreach statement cannot operate on variables of type 'RootObject' because 'RootObject' does not contain a public instance definition for 'GetEnumerator'" Commented Oct 2, 2019 at 6:48
  • When you get this error, how is your foreach statement ? Commented Oct 2, 2019 at 6:50
  • As above: foreach (var p in jsonObj.hireSchedule) { //stuff } Commented Oct 2, 2019 at 6:51
  • 1
    Yeah, that was an error in the copy/paste Commented Oct 2, 2019 at 7:06

2 Answers 2

4

Since you have already defined the classes it's easy enough to deserialize it into them. Then you have a strongly typed class and the IDE should be able to help you out how to access the properties.

var json = File.ReadAllText("json1.json");
var root = JsonConvert.DeserializeObject<RootObject>(json);

foreach (var p in root.hireSchedules)
{
    ///do something
}
Sign up to request clarification or add additional context in comments.

2 Comments

Using this method i receive the following error: Object reference not set to an instance of an object. So, it is not deserializing right.
@jradata It seems like your JSON doesn't match your classes then, even though in the example you have provided above it does.
1

One of the way is to use Newtonsoft.json nuget which is really very powerful, so

var files = JObject.Parse(YourJSON);
var recList = files.SelectTokens("$..hireSchedules").ToList();
foreach (JObject obj in recList.Children())
 {
   foreach (JProperty prop in obj.Children())
      {
        var key = prop.Name.ToString();
        var value = prop.Value.ToString();
         //Do your stuffs here
      }

  }

Comments

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.