MattyAB
MattyAB

Reputation: 375

C# JSON Parse gives error 'System.ArgumentNullException' when parsing JSON array

I'm trying to parse this JSON:

{"error":[],"result":{"XETHXXBT":{"a":["0.023769","64","64.000"],"b":["0.023756","42","42.000"],"c":["0.023756","1.21518360"],"v":["74038.22109284","130984.00945101"],"p":["0.023689","0.006974"],"t":[1272,2332],"l":["0.023440","0.023440"],"h":["0.024091","0.024669"],"o":"0.024084"}}}

The piece of data I want to get is

"c":["0.023756","1.21518360"],

More to the point, the first value of "c". The code I am using is:

    JObject jObject = JObject.Parse(json);
    double balance = (double)jObject["c"];

What am I doing wrong? How can I just get the first value of the "c" array?

Thanks!

Here is the full error report: http://pastebin.com/4S204aUG

Upvotes: 2

Views: 379

Answers (3)

Pankaj Gupta
Pankaj Gupta

Reputation: 388

Use Like that it will give you more flexibility to get the data , if you don't know how may level depth you have to search it will become very difficult and code will be look like tightly coupled or hardcoded whenever you working with json or xml follow class structure it will help to debug or understand the structure as well

public class Test {
  public string[] error { get; set; }
  public string result { get; set; }
}
  
public class Result {
   public List<Values> XETHXXBT { get; set; }
}
  
public class Values {
  public List<Double> a { get; set; }
  public List<Double> b { get; set; }
  public List<Double> c { get; set; }
}

After that class Jsonconvert.deserializeObject<Test>(jsonString); and store it in object you will proper json data in object and after that you can traaverse and get desired data according to your need.Hope it will help you T Thanks

Upvotes: 0

Timothy Ghanem
Timothy Ghanem

Reputation: 1616

To get to c do the following:

var o = JObject.Parse(<yourjsontext>);
var result = o["result"];
var XETHXXBT = result["XETHXXBT"];
var c = XETHXXBT["c"];

var value1 = (double) c[0];
var value2 = (double) c[1];

Upvotes: 3

deltree
deltree

Reputation: 3824

Your problem is that your object is more complicated than it looks.

The answer is jObject['results']['XETHXXBT']['c'][0] or jObject['results']['XETHXXBT']['c'][1]. Take a look at it with a prettifier.

{
   "error":[

   ],
   "result":{
      "XETHXXBT":{
         "a":[
            "0.023769",
            "64",
            "64.000"
         ],
         "b":[
            "0.023756",
            "42",
            "42.000"
         ],
         "c":[
            "0.023756",
            "1.21518360"
         ],
         "v":[
            "74038.22109284",
            "130984.00945101"
         ],
         "p":[
            "0.023689",
            "0.006974"
         ],
         "t":[
            1272,
            2332
         ],
         "l":[
            "0.023440",
            "0.023440"
         ],
         "h":[
            "0.024091",
            "0.024669"
         ],
         "o":"0.024084"
      }
   }
}

As you can see, your base object does not have a c property, so naturally it is throwing an error when you attempt to access (and automatically type cast) that nonexistent property.

Upvotes: 1

Related Questions