-1

I have JSON similar to this . I wish to extract values like name, his id, and product title from this list . But I am not able to figure it out . I was trying "eval" for the same.

{
    "data": [{
        "id": "3092773937557",
        "from": {
            "id": "1810306393",
            "name": "Prashant Singh"
        },
        "start_time": "2012-07-21T09:12:53+0000",
        "end_time": "2012-07-21T09:12:53+0000",
        "publish_time": "2012-07-21T09:12:53+0000",
        "application": {
            "id": "132692593533721",
            "name": "Compare Hatke"
        },
        "data": {
            "productname": "Apple iPod Nano",
            "price": 399,
            "product": {
                "id": "10151004296768984",
                "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
                "type": "comparehatke:product",
                "title": "Apple iPod Nano"
            }
        },
        "likes": {
            "count": 0
        },
        "comments": {
            "count": 0
        },
        "no_feed_story": false
    }, {
        "id": "3092770217464",
        "from": {
            "id": "1810306393",
            "name": "Prashant Singh"
        },
        "start_time": "2012-07-21T09:08:53+0000",
        "end_time": "2012-07-21T09:08:53+0000",
        "publish_time": "2012-07-21T09:08:53+0000",
        "application": {
            "id": "132692593533721",
            "name": "Compare Hatke"
        },
        "data": {
            "productname": "Apple iPod Nano",
            "price": 399,
            "product": {
                "id": "10151004296768984",
                "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
                "type": "comparehatke:product",
                "title": "Apple iPod Nano"
            }
        },
        "likes": {
            "count": 0
        },
        "comments": {
            "count": 0
        },
        "no_feed_story": false
    }],
    "paging": {
        "next": "https:\/\/graph.facebook.com\/me\/comparehatke:compare\/?access_token=AAAB4rubm4xkBAHRhdjVgx7JxIIvUxImIm31AMxgnqEAOQsAsgZAJjBYUfvzKc8XgxDBg3AzKN1S6QU2dnmtgj7TPcoCiih1RzrL3pLpuZAgGt8eKpq&limit=2&method=get&pretty=0&offset=2"
    }
}
Esailija
  • 138,174
  • 23
  • 272
  • 326
Prashant Singh
  • 3,725
  • 12
  • 62
  • 106

2 Answers2

0
data = JSON.parse(yourJSONString);

If this fails, you likely have an error in your JSON. You can use http://jsonlint.com/ to find and resolve the problem. In the paste above, you're missing your closing ]}.

arbales
  • 5,466
  • 4
  • 33
  • 40
  • ie8+.....its better to use json2. (https://github.com/douglascrockford/JSON-js/blob/master/json2.js) – Royi Namir Jul 21 '12 at 08:53
  • Browser implementation bug. I'd say start simple - and start with a good dev environment :D – arbales Jul 21 '12 at 08:58
  • 1
    People shouldn't troll answers with legacy browser stuff unless the OP has specified a requirement for that. IE8 supports `JSON.parse` anyway. – Esailija Jul 21 '12 at 08:59
  • @Esailija I didnt troll an answer , just comment for version support. so if another person will see this message , he will know that. – Royi Namir Jul 21 '12 at 09:23
  • @RoyiNamir ok, sorry. But you could have edited the answer to be better. Not many people read comments :) – Esailija Jul 21 '12 at 09:26
0
<html>
   <script style="text/javscript">
    var myObject = { "data": [{
        "id": "3092741696751",
        "from": {
        "id": "1810306393",
        "name": "Prashant Singh"
    },
     "start_time": "2012-07-21T08:40:38+0000",
     "end_time": "2012-07-21T08:40:38+0000",
     "publish_time": "2012-07-21T08:40:38+0000",
     "application": {
     "id": "132692593533721",
     "name": "Compare Hatke"
     },
     "data1": {
         "productname": "Apple iPod Nano",
         "price": 399,
         "product": {
         "id": "10151004296768984",
         "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
         "type": "comparehatke:product",
         "title": "Apple iPod Nano"
      }
      },
      "likes": {
      "count": 0
      },
      "comments": {
      "count": 0
      },
      "no_feed_story": false
      } ]};
   alert(myObject.data[0].id);
   </script>
 </html>
Dale
  • 1,220
  • 2
  • 10
  • 20
  • I guess what I meant to say is, "json" is simply javascript object literals. – Dale Jul 21 '12 at 09:05
  • Where did you get the idea that "the rest of the web" copied Google with "inline objects"? JSON is _not_ a fundamental data structure to JavaScript, and is _not_ simply JS object literals. JSON is a _string_ representation of data that uses similar syntax to JS object literal syntax. JSON is intended for data transfer; when JavaScript code receives some JSON (most commonly via an Ajax request) it needs to parse or eval it to get an actual JS object. – nnnnnn Jul 21 '12 at 09:05
  • Actually, JSON is a fundamental data structure to js. I don't want to start a flame war here. Think about it, when you send off json via js, you do not have to encode or decode. It is only when you deal with other languages that you start to deal with the encode decode nonsense. Javascript started the json syntax, and that is what it has evolved into today. It absolutely is an object literal in js. Thinking about the stuff otherwise makes any web developers life more difficult. – Dale Jul 21 '12 at 09:14
  • @Dale it says undefined. Neither eval nor ur solution is working – Prashant Singh Jul 21 '12 at 09:15
  • sorry man, updated. I too fell into the, "don't think about it like not a js object trap." Updated – Dale Jul 21 '12 at 09:38
  • there, copy and paste that, you will see the syntax. My point is, this is valid syntax for ANY js object. – Dale Jul 21 '12 at 09:44