-4

I have a large number (235) of JSON strings as follows:

"57": {
    "ID": 6986,
    "Town": "Paris",
    "latitude": 48.8829447,
    "longitude": 2.3453532999999
},
"58": {
    "ID": 6987,
    "Town": "Paris",
    "latitude": 48.8749566,
    "longitude": 2.3588815000001
}

And I would like to make a JavaScript array. I have read many posts on the Internet about it, but all of them seem to be not appropriate for my case. What should I do?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hubert Solecki
  • 2,611
  • 5
  • 31
  • 63
  • This JSON isn't complete - I don't see any array syntax (`[` and `]`). – Chris Laplante Mar 21 '13 at 21:36
  • 3
    This isn't valid JSON. Did you forget the outer curly braces? – Asad Saeeduddin Mar 21 '13 at 21:41
  • 2
    Without any more information, this is a duplicate of http://stackoverflow.com/q/4935632/218196. Please post a more complete example of your data. What exactly is this "list"? Is it a data structure in a programming language, or is it a textual list? How exactly does it look like? What have you tried so far and why didn't work? Which error did you get? – Felix Kling Mar 21 '13 at 21:44
  • i've encoded a php array like that $jsarray = json_encode($tab_LatLong); and it gives that result... – Hubert Solecki Mar 21 '13 at 21:47
  • 2
    @HubertSolecki It isn't possible that you get exactly the result you have posted, since the result you have posted isn't valid JSON. If what you have posted is just a chunk of the output, you should explicitly state that. – Asad Saeeduddin Mar 21 '13 at 21:49
  • So assuming you just posted a part of the JSON, and there are surrounding `{...}` somewhere, this really is a duplicate. If you are rather asking for how to *access* the data once it is parsed, have a look at this question: http://stackoverflow.com/q/11922383/218196. – Felix Kling Mar 21 '13 at 21:52
  • When posting truncated JSON, make sure it is still valid, [jsonlint.com](http://jsonlint.com/) is handy for validating JSON. – Useless Code Mar 21 '13 at 21:58
  • I'm sorry, i'm new in JSON syntax, i have found out a solution where i'm not Using JSON because, as you all said, my JSON output wasn't valid ! Thank you all for the answers ! – Hubert Solecki Mar 22 '13 at 10:11

2 Answers2

3

You can use JSON.parse()

var javascriptObject = JSON.parse(jsonString);

In older browsers you will need to include the json2.js library:

Download Link

Oliver
  • 8,794
  • 2
  • 40
  • 60
3

The JSON you posted is a bit broken, looking at it I'm assuming that it is supposed to be an object with properties containing objects:

{
    "57": {
            "ID": 6986,
            "Town": "Paris",
            "latitude": 48.8829447,
            "longitude": 2.3453532999999
    },
    "58": {
            "ID": 6987,
            "Town": "Paris",
            "latitude": 48.8749566,
            "longitude": 2.3588815000001
    }
}

Assuming this, you would first need to convert it to a JS object using JSON.parse:

var obj = JSON.parse(jsonString);

Then you need to turn this object into an array:

var i,
  arr = [];

for (i in obj) {
  if (obj.hasOwnProperty(i)) {
    arr.push(obj[i]);
  }
};

// arr now contains the array you were trying to get
Useless Code
  • 12,123
  • 5
  • 35
  • 40