0

I'm embarrassed to ask but I'm really having trouble getting the values out of some JSON. I just can't seem to access the array of values correctly.

I can get the values from this Json (fiddle: working jsfiddle) which uses the json response in the format below:

{
    "items": [
        {
            "tags": [
                "jqgrid"
            ],
            "owner": {
                "reputation": 21,
                "user_id": 3038042,
                "user_type": "registered",
                "accept_rate": 70,
                "profile_image": "https://www.gravatar.com/avatar/819ef95bfca002204f5bd00654fb9957?s=128&d=identicon&r=PG&f=1",
                "display_name": "Amete",
                "link": "http://stackoverflow.com/users/3038042/amete"
            },
            "is_answered": false,
            "view_count": 5,
            "answer_count": 1,
            "score": 1,
            "last_activity_date": 1423648956,
            "creation_date": 1423605704,
            "last_edit_date": 1423648956,
            "question_id": 28442722,
            "link": "http://stackoverflow.com/questions/28442722/jqgrid-set-focus-on-selectrow-not-working",
            "title": "JqGrid Set Focus on SelectRow not working"
        }
    ]
}

BUT I can't do the same for the Json below (failed jsfiddle)

[{
    "Manufacturer": "Toyota",
    "Sold": 1200,
    "Month": "2012-11"
}, {
    "Manufacturer": "Ford",
    "Sold": 1100,
    "Month": "2012-11"
}, {
    "Manufacturer": "BMW",
    "Sold": 900,
    "Month": "2012-11"
}, {
    "Manufacturer": "Benz",
    "Sold": 600,
    "Month": "2012-11"
}]

I know I am not accessing the array correctly but everything I've tried has failed.

Additionally, it looks simpler to use the JQuery $.getJSON approach but I can't make that work either...

Any advice would be much appreciated.

ChrisCurrie
  • 1,589
  • 6
  • 15
  • 36
  • 2
    The failed fiddle doesn't get any data, it's stopped by the same-origin policy – adeneo Feb 11 '15 at 12:38
  • Of course... foolish of me... In reality I'm actually using a web service - I just used the example JSON for simplicity. I thought the web service got around the whole 'same-origin policy' but, taking the approach of @TrueBlueAussie below I see that copying the data out of my service and declaring it as a variable works. This was the main reason I could not get any responses. Can you explain why the stackoverflow API doesn't suffer from the same-origin policy but my API does? http://goo.gl/m0SQB4 ? – ChrisCurrie Feb 11 '15 at 13:07
  • OK. So ultimately I have been suffering from same-origin policy. This has made testing my APIs and utilising them locally (in dev) impossible. Ultimately I ended up enabling CORS on the server which resolved the issues below and enabled my tests using a real API work. – ChrisCurrie Feb 11 '15 at 15:51
  • I'm using .NET and WebAPI so here's the article that resolved it for me: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api – ChrisCurrie Feb 11 '15 at 15:59

2 Answers2

0

Your second set of data is a pure array with no named element called item at the top level.

Use

var item = result[i];

not

var item = result.item[i];

JSFiddle: http://jsfiddle.net/am77Lm8w/3/

I turned your Ajax callback into an IIFE to test the data.

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
  • Thanks @TrueBlueAussie! That works well but I tried your approach with my real web service (rather than the example) and still got no data. I presume this is down to the same-origin policy restrictions but I thought web service API's got around that? The lack of data was the cause of my multiple failed attempts to get a result prevoiusly. Here's a working fiddle based on your approach here: http://jsfiddle.net/yokmtpco/ Failed fiddle using the web service here: http://jsfiddle.net/qqxwuse0/. It's clear I must seriously up-skill here. – ChrisCurrie Feb 11 '15 at 13:17
  • Both those JSFiddles look the same to me. Was the second one supposed to use a webservice? – iCollect.it Ltd Feb 11 '15 at 13:50
  • Yes, thanks @TrueBlueAussie, it should have been. Odd... I had two open windows... both the same URL. It didn't update apologies. Here's the correct version http://jsfiddle.net/qqxwuse0/1/ – ChrisCurrie Feb 11 '15 at 14:07
  • That one has a missing `}` on the `success:` function. Good indenting is your friend :) http://jsfiddle.net/qqxwuse0/2/ – iCollect.it Ltd Feb 11 '15 at 14:09
  • Yes, thanks. I did think I'd better just check using JSHint but you were so fast you beat me to it. :) Still no response outputted though? Although there is when the JSON is declared as a variable... – ChrisCurrie Feb 11 '15 at 14:15
  • If you check the console output you will see it is failing due to cross domain security. You will need to allow access to that server or start using JSONP.. – iCollect.it Ltd Feb 11 '15 at 14:18
0

There is no items in your second json.

You need to use var item = result[i];

Alexandros B
  • 1,881
  • 1
  • 23
  • 36