-2

I have this JSON:

{
  "result": {
    "data": {
      "getEstelam3Response": {
        "return": {
          "birthDate": "13650412",
          "bookNo": "0",
          "bookRow": [ "0" ],
          "deathStatus": [ "0" ],
          "family": "2K3Yp9is2YrYp9mG2Yo\u003d",
          "fatherName": "2K3Zitiv2LE\u003d",
          "gender": "1",
          "message": "",
          "name": "2LXYr9ix2Kc\u003d",
          "nin": "3549970269",
          "officeCode": "309",
          "shenasnameNo": "442",
          "shenasnameSerial": "0"
        }
      }
    },
    "status": {
      "statusCode": 200,
      "message": "OK"
    }
  },
  "status": {
    "statusCode": 200,
    "message": "OK"
  }
}

I want to get value of keys inside return.

This is my code:

var obj = JObject.Parse(responseBody);
string name = (string)obj.SelectToken("result[0].data[0].getEstelam3Response[0].return[0].birthDate[0]");

but when I write

Response.Write(name);

I get nothing.

I want all values inside result.

please help. much appreciated.

2 Answers 2

1

You're trying to index into objects like arrays ([0]), but in your JSON structure, result, data, getEstelam3Response, and return are all JSON objects—not arrays. Here is a probably correct way to access the values inside the return object:

var obj = JObject.Parse(responseBody); 
var returnObj = obj.SelectToken("result.data.getEstelam3Response.return"); 
string birthDate = (string)returnObj["birthDate"]; 
string bookRow = returnObj["bookRow"][0].ToString(); 
Response.Write("BirthDate: " + birthDate + "<br>");
Sign up to request clarification or add additional context in comments.

2 Comments

i tried your code. it says object reference not set to instance of ab object. null reference.
Hi @Mike, jialin.zhou's answer is worked in this demo. Are you sure your actual JSON response body is same as what you posted in the question? If the Newtonsoft.Json.Linq is too complex (sometimes may have typo error), it would be recommended to create a class that follows the JSON structure and then you deserialize the JSON to the (created) class instance.
0

The below approach will print all its keys and respective values accordingly as well as its individual values.

You can use the below approach:

using System;
using System.Text;
using Newtonsoft.Json.Linq;

class Program
{
    static void Main()
    {
        string json = @"
        {
            ""result"": {
                ""data"": {
                    ""getEstelam3Response"": {
                        ""return"": {
                            ""birthDate"": ""13650412"",
                            ""bookNo"": ""0"",
                            ""bookRow"": [ ""0"" ],
                            ""deathStatus"": [ ""0"" ],
                            ""family"": ""2K3Yp9is2YrYp9mG2Yo\u003d"",
                            ""fatherName"": ""2K3Zitiv2LE\u003d"",
                            ""gender"": ""1"",
                            ""message"": """",
                            ""name"": ""2LXYr9ix2Kc\u003d"",
                            ""nin"": ""3549970269"",
                            ""officeCode"": ""309"",
                            ""shenasnameNo"": ""442"",
                            ""shenasnameSerial"": ""0""
                        }
                    }
                },
                ""status"": {
                    ""statusCode"": 200,
                    ""message"": ""OK""
                }
            },
            ""status"": {
                ""statusCode"": 200,
                ""message"": ""OK""
            }
        }";

        JObject obj = JObject.Parse(json);
        var returnData = obj.SelectToken("result.data.getEstelam3Response.return");
        if (returnData == null)
        {
            Console.WriteLine("Could not find 'return' object.");
            return;
        }
        string DecodeBase64(string encoded)
        {
            try
            {
                byte[] data = Convert.FromBase64String(encoded);
                return Encoding.UTF8.GetString(data);
            }
            catch
            {
                return encoded; 
            }
        }
        // For individual value fetching        
        string birthDate = (string)returnData["birthDate"];
        string bookNo = (string)returnData["bookNo"];
        string name = (string)returnData["name"];
        string family = (string)returnData["family"];
                Console.Write($"Name: {name}, BirthDate: {birthDate}, Family: {family}");

        // For dynamically fetching values
                foreach (var prop in returnData.Children<JProperty>())
                {
                    string value = prop.Value.Type == JTokenType.Array
                        ? string.Join(", ", prop.Value.ToObject<string[]>())
                        : prop.Value.ToString();
                    if (prop.Name is "name" or "family" or "fatherName")
                    {
                        value = DecodeBase64(value);
                    }
                    Console.WriteLine($"{prop.Name}: {value}");
                }
    }
}

Please find the working .netfiddle 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.