简体   繁体   中英

How to convert from a JSON array to a JSON object

I have this JSON that I'm getting back from a web service call:

{
    "name": "My Name",
    "path": "my path",
    "id": "44",
    "type": "my type",
    "classes": "my classes"
},
{
    "name": "his Name",
    "path": "his path",
    "id": "76",
    "type": "his type",
    "classes": "his classes"
}

I then need to convert it to this format

{
    "44" : { "name" : "My Name", "path" : "my path" },
    "76" : { "name" : "his Name", "path" : "his path" }
}

My initial naive attempt was this:

var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push({
    rawData[i].id :
    {
        "path": rawData[i].path,
        "name": rawData[i].name
    }
});

which fails with syntax errors, so I eventually got to this:

var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push(rawData[i].id,
{
    "path": rawData[i].path,
    "name": rawData[i].name
});

and it mostly works. My array is populated, but the problem is that my myData array doesn't have the "44", and "76" part of the object, just the { "name" : "", "path" : "" } part. I expect this is due to a lack of understanding on my part of how JSON and javscript objects work.

Don't use Array.prototype.push() , use the square bracket notation and define your output as an object not an array.

var myData = {};
for (var i = 0; i < rawData.length; i++) {
    myData[rawData[i].id] = {
        "path": rawData[i].path,    
        "name": rawData[i].name
    }
}

Your desired output isn't an array, so that's your starting point. The output you've said you want is an object , not an array.

You build your result by creating a blank object and then adding the objects to it using id as the key:

var myData = {};
rawData.forEach(function(entry) {
    myData[entry.id] = {
        name: entry.name,
        path: entry.path
    };
});

Or if you don't want to use forEach (it's ES5, but can be shimmed for older browsers), the old-fashioned way:

var myData = {};
var index, entry;
for (index = 0; index < rawData.length; ++index) {
    entry = rawData[index];
    myData[entry.id] = {
        name: entry.name,
        path: entry.path
    };
}

You need to convert your id to a string?

var myData = {};
for (var i = 0; i < rawData.length; i++) {
    myData[String(rawData[i].id)] = {
        "path": rawData[i].path,
        "name": rawData[i].name
    };
}

A variation on what other posters have written:

// Create a new empty object.
var out = {};

// Loop over your array of objects
// Add the each object id as a key to the output object, and the object as the value.
for (var i = 0, l = arr.length; i <l; i++) {
  var obj = arr[i];
  out[obj.id] = obj;

  // Delete the properties from the newly added object you don't want.
  delete obj.id;
  delete obj.type;
  delete obj.classes;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM