0

This is the JSON I am trying to read:

[{
    "model": "catalog.product",
    "pk": 2476,
    "fields": {
        "created": "2016-10-04T05:28:48.270Z",
        "updated": "2016-10-04T05:28:48.270Z",
        "name": "Flucloxin 250mg",
        "slug": "flucloxin-250mg-1-09353813995272736",
        "generic": "",
        "power": "",
        "manufacturer": null,
        "sku": "",
        "price": "0.00",
        "old_price": "0.00",
        "is_active": true,
        "unit": "piece",
        "description": null,
        "meta_keywords": "",
        "meta_description": "",
        "category": 1,
        "subcategory": 1,
        "height": null,
        "width": null,
        "image": "",
        "thumbnail": "",
        "dosage": 1,
        "related": []
    }
}]

I have tried using $.get, $.getJSON, etc. but the example I am following is reading from a JSON format like the following:

{
  "one": "Singular sensation",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

As you can see this is only one dict, however I am planning to read a bunch of dicts within a list. Also, please note that there is a dict within the dict under the key fields.

How can I read the above format?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
MiniGunnR
  • 5,590
  • 8
  • 42
  • 66
  • 3
    "The above format" is valid JSON, and should be parsed by `$.getJSON`; you can also parse it manually using `JSON.parse`. Do you mean how to access individual objects in the parsed data structure...? – Amadan Oct 11 '16 at 06:30
  • Yes. I want to loop through the dicts in the JSON I provided with square brackets. However http://api.jquery.com/jquery.getjson/ this link shows the use of getJSON on the single dict JSON. So it's not working for me. – MiniGunnR Oct 11 '16 at 06:34
  • 1
    You can use `for(var i = 0; i < data.length; i++) { var obj = data[i]; ... }`, or `data.forEach(function(obj, i) { ... });` to loop. Please don't say "not working for me" - show your code and state exactly what happens that you don't want, or what doesn't happen that you do. – Amadan Oct 11 '16 at 06:36
  • The square brackets (`[` and `]`) are notations used in `JavaScript` (and `JSON`) for arrays. You may [read more here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) for a detailed explanation. Refer [this sample code here](https://jsbin.com/qacecok/edit?html,js,output). – Kamran Ahmed Oct 11 '16 at 06:41

0 Answers0