0

I need to parse a JSON, I am already parsing the first part of the record but I am having a problem with a sub record. This is my code:

  List<JToken> results = new List<JToken>();
        List<JToken> results2 = new List<JToken>();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
            results = JObject.Parse(result).SelectToken("record").ToList();
        }
        List<Record> users = new List<Record>();
        foreach (JObject token in results)
        {
            Record user = new Record();
            user.id = Int32.Parse(token["id"].ToString());
            user.full_name = token["full_name"].ToString();
            user.email = token["email"].ToString();

            //role.RoleName = token.SelectToken("name").ToString();

        }

That's working perfectly but I have issues parsin a string that's a bit deeper. This is the JSON:

{
  "record": [
   {
  "id": 2,
  "institution_id": 1,
  "full_name": "",
  "email": "",
  "role_id": 2,
  "created": "2015-01-13 01:18:52.370379",
  "updated": "2015-01-22 23:58:44.103636",
  "branch_id": 1,
  "Branch_by_branch_id": {
    "id": 1,
    "institution_id": 1,
    "branch_name": "Test Branch"
  }
}

] }

I want to get the "branch_name" inside Branch_by_branch_id. How can I access it with Jobject?

6
  • Your JSON is not valid. Are you sure you copy/pasted it correctly? Commented Jan 23, 2015 at 0:46
  • 1
    token["Branch_by_branch_id"]["branch_name"] should do the trick, will test when the JSON is fixed. Commented Jan 23, 2015 at 0:47
  • Tested that out DBC, gave me a " Object reference not set to an instance of an object. " error Commented Jan 23, 2015 at 0:49
  • 1
    @LordRelix - just tried var branch_name = token["Branch_by_branch_id"]["branch_name"].ToString() and it worked. Did you do something different? Commented Jan 23, 2015 at 0:54
  • 1
    Of course, checking for missing records is reasonable: if (token["Branch_by_branch_id"] != null && token["Branch_by_branch_id"]["branch_name"] != null) Commented Jan 23, 2015 at 0:56

1 Answer 1

1

If your JSON is this

{
    "record": [
        {
            "id": 26,
            "full_name": "",
            "email": "",
            "branch_id": 1,
            "Branch_by_branch_id": {
                "id": 1,
                "institution_id": 1,
                "branch_name": "NAME"
            }
        }
    ]
}

Have classes like this:

public class BranchByBranchId
{
    public int id { get; set; }
    public int institution_id { get; set; }
    public string branch_name { get; set; }
}

public class Record
{
    public int id { get; set; }
    public string full_name { get; set; }
    public string email { get; set; }
    public int branch_id { get; set; }
    public BranchByBranchId Branch_by_branch_id { get; set; }
}

public class RootObject
{
    public List<Record> record { get; set; }
}

Then parse it and retrieve the value like this.

var root = JsonConvert.DeserializeObject<RootObject>(json);
var branchName = root[0].Branch_by_branch_id.branch_name;

I always prefer to access my JSON objects like this, because I like having my objects as native C# classes. The classes were generated by json2csharp.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Mason, will try this out. I take it the root index has to be changed for a integer variable so I can get the records of each loop kight?
@LordRelix Yes, or you could do something like foreach(var record in root.record) { Console.WriteLine(record.Branch_by_branch_id.branch_name); }

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.