3

i am working in C#,where I created one small project.In the project I am parsing one json file.I just want to ask that can I parse or use the json file like.....

JSON File

{
"Nodes": [
      {
        "Number of Location":"4",
      },
      {
        "longitude": "22.3 ",
        "latitude": "45.3",
        "Demand": "5"
      },
      {
        "longitude": "26.3 ",
        "latitude": "46.3",
        "Demand": "6"
      },
      {
        "longitude": "35.1 ",
        "latitude": "48.2",
        "Demand": "2"
      }
]
}

C# code

 string path = null;
 Console.WriteLine("enter the path where order matrix is loacated");
 path = Console.ReadLine();
 System.IO.StreamReader myFile = new System.IO.StreamReader(path);
 string myString = myFile.ReadToEnd();
 JObject o = JObject.Parse(myString);
 Console.WriteLine(o["Nodes"][0]["Number of Location"]);
 for (int i = 0; i < 5; i++)
 {
     Console.WriteLine(o["Nodes"][i+1]["longitude"]);
     Console.WriteLine(o["Nodes"][i+1]["latitude"]);
     Console.WriteLine(o["Nodes"][i+1]["Demand"]);
 }   

I am not getting the value of Number of location when i am parsing the JSON file.so can you help that how can I fetch the value of number of location..

6
  • Are you getting a null value? Commented Jun 2, 2015 at 12:13
  • No,actually when I am getting string from the file then that string doesn't contain the "Number of Location" field but all the renaming field is fetching. Commented Jun 2, 2015 at 12:17
  • What does the field contains or prints out? May you post it here? Commented Jun 2, 2015 at 12:20
  • I believe it is because of the spaces in the "Number of location" Commented Jun 2, 2015 at 12:25
  • the json is not valid.The Array seems invalid. Commented Jun 2, 2015 at 12:51

1 Answer 1

2

Well you should be using JArray to parse a json Array , the code would look like this :

        var res = JObject.Parse(text)["Nodes"];
        foreach (var o in res.Children<JObject>())
        {
            foreach (var property in o.Properties())
            {
                Console.WriteLine(property.Name + " " + property.Value);
            }
        }

if you want just the number of location use this:

        var res = JObject.Parse(text)["Nodes"];
        Console.WriteLine(res[0]["Number of Location"]);
Sign up to request clarification or add additional context in comments.

7 Comments

Its not an array its an object.
He pasted the whole file. It is literally an object with a single property of type array. But a naked property is not a valid top level JSON structure. Anyway, the you don't need to call JArray.Parse because the property has already been desearilized into a JArray by the call to JObject.Parse.
No the result of JObject.Parse is a JObject not a JArray
Your code parses the file into a JObject, uses the indexer to access the Nodes property which is of type JArray, re-serializes that JArray by calling ToString, and then parses it back into a JArray. Just write this: var res =(JArray)JObject.Parse(text)["Nodes"]
in what way this code is different than mine,,, you said Jobject.Parse(text) is a JArray when actually it s not,, anyway I ll just take away the JArray.Parse , to make the code simpler
|

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.