Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am using Json.NET in order to parse a json string but when i try to use SelectToken it returns null. Also it seems that Json.NET read the json string as 1 node

here is the JSON

[
   [{
      "input_index":0,
      "candidate_index":0,
      "delivery_line_1":"124 Main St",
      "last_line":"Cambridge MA 02138-5813",
      "delivery_point_barcode":"021385813991",
      "components":{
         "primary_number":"125",
         "street_name":"Main",
         "street_suffix":"St",
         "city_name":"Cambridge",
         "state_abbreviation":"MA",
         "zipcode":"02138",
         "plus4_code":"5813",
         "delivery_point":"99",
         "delivery_point_check_digit":"1"
      },
      "metadata":{
         "record_type":"H",
         "county_fips":"25017",
         "county_name":"New York",
         "carrier_route":"C025",
         "congressional_district":"08",
         "building_default_indicator":"Y"
      },
      "analysis":{
         "dpv_match_code":"D",
         "dpv_footnotes":"AAN1",
         "dpv_cmra":"N",
         "dpv_vacant":"N",
         "ews_match":false,
         "footnotes":"A#H#N#"
      }
   }]
]

and here is the code

JArray o = JArray.Parse(page);
string something = (string)o.SelectToken("county_name");

However it just returns me null and i am totally confused as there is something wrong with JSON.NET parsing this Json string

share|improve this question
    
It's an array... an array doesn't have a county_name... – Jeff Mercado Jul 7 '13 at 19:40
    
it is under metadata – Roman Ratskey Jul 7 '13 at 19:42
    
So how could i retrieve it... – Roman Ratskey Jul 7 '13 at 19:43
    
Also they are double arrays. outer and inner one. – Roman Ratskey Jul 7 '13 at 19:44
up vote 2 down vote accepted

See: What is the JSON.NET equivilant of XML's XPath, SelectNodes, SelectSingleNode?

var data = GetJson();

//You can use the SelectToken to get the value similar to XPath.
var value = JArray.Parse(data)
                  .SelectToken("[0][0].metadata.county_name")
                  .Value<string>();

This can be extended to support multiple elements:

var jArray = JArray.Parse(data);
var countyNames = new List<string>();

foreach(var element in jArray.SelectToken("[0]"))
{
    var value = element.SelectToken("metadata.county_name").Value<string>();

    countyNames.Add(value);
}
share|improve this answer
    
what is the use of [0][0] – Roman Ratskey Jul 7 '13 at 21:12
    
First element of the unnamed outer array and first element of the unnamed inner array. – Romoku Jul 7 '13 at 21:14
    
But i still get null using this code idk why!. i am sorry but could you have a little patience with me to solve this – Roman Ratskey Jul 7 '13 at 21:16
    
Is you are getting null then either the JSON that you are receiving is different, it does not contain the same property, or there is a typo in your code. – Romoku Jul 7 '13 at 21:19
    
i am trying on the same JSON posted... – Roman Ratskey Jul 7 '13 at 21:19

So, the issue is that you basically have a wrapper around your actual object. You basically have:

Array of objects
  Object
    Metadata
    etc

So basically, get the first JToken out of the JArray and access the metadata from there.

share|improve this answer
1  
Please make you answer richer with a code so another people after me could find it useful – Roman Ratskey Jul 7 '13 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.