-3

i get json data from api like this :

{
    "function": "getCategoryItem",
    "version": "0.1",
    "returnCode": true,
    "errorCode": 0,
    "errorMessage": "",
    "result": {
        "_ITEM_": [
            {
                "itemId": 10000,
                "itemName": "Item Test\u00fc\u00e7\u011e\u00d6",
                "itemImageURL": "test.png",
                "itemDescription": "21testDESC",
                "itemPrice": 762
            },
            {
                "itemId": 10001,
                "itemName": "Item Test V2",
                "itemImageURL": "tiger.png",
                "itemDescription": "asfdjKDEWSC",
                "itemPrice": 552
            },
                        ],
        "_NAVIGATION_": [
            {
                "itemCount": 21,
                "totalCount": 21,
                "currentPage": 1,
                "navigationSize": 10,
                "pageSize": 10
            }
        ]
    }
}

But i cant access ITEM -> itemId or NAVIGATION -> currentPage how can i access inside to ITEM and NAVIGATION using JavaScript? Thanks for your help.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117

1 Answers1

0

First, the JSON in the question (if it's really meant to be JSON, not just an object literal) is invalid: It has a trailing comma after the first entry in _ITEM_, which is valid in JavaScript but not in JSON.

Once that's fixed, and assuming at some stage you parse the JSON, at that point it's no longer JSON, its an object with properties (which refer to other objects, etc.). In your case, if data refers to the entire parsed result, you'd access _ITEM_ like this:

console.log(data.result._ITEM_[0].itemId); // 10000

Live example:

var json =
    '{\n'+
    '    "function": "getCategoryItem",\n' +
    '    "version": "0.1",\n' +
    '    "returnCode": true,\n' +
    '    "errorCode": 0,\n' +
    '    "errorMessage": "",\n' +
    '    "result": {\n' +
    '        "_ITEM_": [\n' +
    '            {\n' +
    '                "itemId": 10000,\n' +
    '                "itemName": "Item Test\u00fc\u00e7\u011e\u00d6",\n' +
    '                "itemImageURL": "test.png",\n' +
    '                "itemDescription": "21testDESC",\n' +
    '                "itemPrice": 762\n' +
    '            },\n' +
    '            {\n' +
    '                "itemId": 10001,\n' +
    '                "itemName": "Item Test V2",\n' +
    '                "itemImageURL": "tiger.png",\n' +
    '                "itemDescription": "asfdjKDEWSC",\n' +
    '                "itemPrice": 552\n' +
    '            }\n' +
    //            ^-- This is where the invalid comma was
    '                        ],\n' +
    '        "_NAVIGATION_": [\n' +
    '            {\n' +
    '                "itemCount": 21,\n' +
    '                "totalCount": 21,\n' +
    '                "currentPage": 1,\n' +
    '                "navigationSize": 10,\n' +
    '                "pageSize": 10\n' +
    '            }\n' +
    '        ]\n' +
    '    }\n' +
    '}';
var data = JSON.parse(json);
console.log(data.result._ITEM_[0].itemId); // 10000
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Can i ask one more question, do you know how can i get count of result, _ITEM_ variables. Like: _ITEM_ = 2; _NAVIGATION_ = 1; result = 2; – Ahmet Çelikezer Dec 31 '17 at 14:32
  • @AhmetÇelikezer: `_ITEM_` and `_NAVIGATION_` are just arrays. Like all arrays, they have a `length` property, e.g.: `data.result._ITEM_.length`. (`result` is a non-array object. They don't have a length. You can get the total number of *own, enumerable* properties it has [well, those with string names, which they will if this was originally JSON] from `Object.keys(result).length`.) – T.J. Crowder Dec 31 '17 at 14:34
  • 1
    Okay, that works! thank you so much :) – Ahmet Çelikezer Dec 31 '17 at 14:38