1

I am having trouble working with an API, first time I've worked with one. I've managed to use GET to read the data I need and that part is working perfectly, now I need to deserialize the JSON data I am getting back, and I am using Newtonsoft's JSON .NET library. The problem seems to come when I deserialize the data as its exploding and the debugger isn't being helpful. I tried a few suggestions online but no go, so if someone can enlighten me it'd be appreciated. This is the code:

 string url = "";
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 Stream receiveStream = response.GetResponseStream();
 StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

 string responseData = readStream.ReadToEnd();

 var results = JsonConvert.DeserializeObject<dynamic>(responseData);
 var id = results["id"].Value;
 // var name = results.Name;

When I run it the debugger throws the following exception at the last line of code:

{"Accessed JArray values with invalid key value: \"id\". Array position index expected."}

I am fairly sure that ID exists in the data I am getting back.

Json data I am getting back from Smarty Streets:

 [
{
    "id": 0,
    "candidate_index": 0,
    "delivery_line_1": "1600 Amphitheatre Pkwy",
    "last_line": "Mountain View CA 94043-1351",
    "delivery_point_barcode": "940431351000",
    "components": {
        "primary_number": "1600",
        "street_name": "Amphitheatre",
        "street_suffix": "Pkwy",
        "city_name": "Mountain View",
        "state_abbreviation": "CA",
        "zipcode": "94043",
        "plus4_code": "1351",
        "delivery_point": "00",
        "delivery_point_check_digit": "0"
    },
   ]
8
  • Seems lke your response is JArray not JObject. Try JArray results = JArray.Parse(responseData). Commented Aug 6, 2014 at 15:59
  • 1
    Can you show a json result? Commented Aug 6, 2014 at 16:01
  • 1
    If you have the JSON and it's always the same format it's usually easier to use json2csharp to create the .NET classes and deserialize directly into classes. Commented Aug 6, 2014 at 16:06
  • 1
    It is indeed JArray. Use results[0]["id"] Commented Aug 6, 2014 at 16:07
  • Hi LB! Your answer didn't work. webnett I posted the JSON. Craig... will look into that, thanks. Commented Aug 6, 2014 at 16:08

3 Answers 3

2

Your response is an array not a single object. So You should use

JArray results = JArray.Parse(responseData)

to parse the result (like results[0]["id"]). If you go the dynamic way then

dynamic results = JArray.Parse(responseData)

Now, you can use it like results[0].id

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

Comments

1

Another option is to "Paste JSON as classes" so it can be deserialised quick and easy.

  1. Simply copy your entire JSON
  2. In VS: Click Edit > Paste Special > Paste JSON as classes

Here is a better explanation n piccas... https://blogs.msdn.microsoft.com/webdev/2012/12/18/paste-json-as-classes-in-asp-net-and-web-tools-2012-2-rc/

Comments

0

Your result appears to be an array of objects, not an array itself. Try setting id to results["id"][0].Value and see if you get something.

See this: Accessed JArray values with invalid key value: "fields". Array position index expected

2 Comments

Tried this, no go. I actually thought this would do it. Error nit gave me: {"Accessed JArray values with invalid key value: \"id\". Array position index expected."}
I had written it backwards, sorry. Should have been results[0]["id"], not how I had done it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.