简体   繁体   English

将JSON转换为关联数组

[英]Convert JSON to Associative array

I want to convert a JSON response to associative array but looks like I was not able to make it right. 我想将JSON响应转换为关联数组,但看起来我无法使其正确。

Here is my JSON sample response: 这是我的JSON示例响应:

{
   "response":[
  {
     "id":100,
     "certificate":{
        "id":1,
        "title":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."

     },
     "created_at":"2013-12-02T15:20:08.233Z"

  },
  {
     "id":101,
     "certificate":{
        "id":2,
        "title":"Aenean facilisis, nisl vitae pulvinar varius."
     },
     "created_at":"2013-12-02T15:20:08.240Z"

  }
 ],
"count":2
}

This is what I have tried so far: 这是我到目前为止所尝试的:

    var len = obj.response.length;
    var rData = [];
    var gcData = [];


    for(var i = 0; i < len; i++){

        rData[i] = $.map(obj.response[i], function(value, index) {

            if(typeof value=="object"){
                gcData = $.map(value, function(value1, index) {
                    return [value1];
                });

                return gcData;

            }else{              
                return [value];
            }
        });     

    }

My expected output: 我的预期产量:

rData = [
    100 : [
           id: ["100"],
           certificate: [
              id: ["1"],
              title: ["Lorem ipsum dolor sit amet, consectetur adipiscing elit."]
           ],
           created_at: ["2013-12-02T15:20:08.240Z"]   

         ]
    101 : [
           id: ["101"],
           certificate: [
              id: ["2"],
              title: ["Aenean facilisis, nisl vitae pulvinar varius."]
           ],
           created_at: ["2013-12-02T15:20:08.240Z"]   

         ]

]

Please help. 请帮忙。 Thanks. 谢谢。

I just want to group it by keys something like I have in expected output 我只是想按照我预期输出中的键来分组

It seems you want to create an id -> object map. 看来你想创建一个id -> object图。 To do that you just have to iterate over the array, take the id attribute of each object as property name and assign the object (the element of the array) to that property of the map. 要做到这一点,你只需要迭代数组,将每个对象的id属性作为属性名称,并将对象(数组的元素)分配给地图的该属性。

Example: 例:

var map = {};
var response = obj.response;

for (var i = 0, l = response.length; i < l; i++) {
    map[response[i].id] = response[i];
}

console.log(map);

Each object inside the array is already in the structure you want it to be. 数组中的每个对象都已经在您想要的结构中。 The output is 输出是

{
    "100": {
        "id": 100,
        "certificate": {
            "id": 1,
            "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        },
        "created_at": "2013-12-02T15:20:08.233Z"
    },
    "101": {
        "id": 101,
        "certificate": {
            "id": 2,
            "title": "Aenean facilisis, nisl vitae pulvinar varius."
        },
        "created_at": "2013-12-02T15:20:08.240Z" 
    }
}

your desired JSON is incorrect. 您所需的JSON不正确。

It isn't clear if you want to create an array of arrays or an object of arrays or what. 目前尚不清楚是否要创建数组数组或数组对象或什么。 To help decide which option fits your need, try this code 要帮助确定哪个选项符合您的需求,请尝试使用此代码

var a=[];
var o={};

for (var i=0, l=response.length; i<l; i++) {
  var e = response[i];
  a[e.id] = e.certificate;
  o[e.id] = e.certificate;
}

console.log(JSON.stringify(a));
console.log(JSON.stringify(o));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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