完全重新格式化json响应

时间:2011-09-08 15:40:24

标签: jquery json

我有一个.json响应需要重新格式化为一个不同的响应,我最终会将其重新分析并保存,以便在我的代码的其他部分使用。

我不完全确定如何描述我在这里做的事情,但在我知道我的数组以正确的格式分组后,我可以解析json。 : - \

请允许我向您展示代码,以及输出的外观:

当前的json格式:


    {"response":
      {"docs":[
        {"set_number":"1","set_description":"first description","subset_number":"835","subset_description 1":"hello 2" },
        {"set_number":"1","set_description":"first description","subset_number":"845","subset_description 2":"hello 3" },
        {"set_number":"2","set_description":"first description","subset_number":"855","subset_description 3":"hello 4" },
        {"set_number":"2","set_description":"first description","subset_number":"865","subset_description 4":"hello 5" },
        {"set_number":"3","set_description":"first description","subset_number":"875","subset_description 5":"hello 6" },
        {"set_number":"4","set_description":"first description","subset_number":"885","subset_description 6":"hello 7" },
        {"set_number":"4","set_description":"first description","subset_number":"895","subset_description 7":"hello 8" },
        ]
        }
        }

..这就是我试图让它看起来像:


        {"response":
          {"docs":[

    {
    "set_number":"1","set_description":"first description",
    "subsets":[
        {"subset_number":"845","subset_description 2":"hello 3"},
        {"subset_number":"835","subset_description 1":"hello 2"}
        ]
    },
    {
    "set_number":"2","set_description":"first description",
    "subsets":[
        {"subset_number":"855","subset_description 3":"hello 4"},
        {"subset_number":"865","subset_description 4":"hello 5"} 
    },
    {
    "set_number":"3","set_description":"first description",
    "subsets":[
        {"subset_number":"875","subset_description 5":"hello 6"}
        ]
    },

    {
    "set_number":"4","set_description":"first description",
    "subsets":[
        {"subset_number":"895","subset_description 7":"hello 8"},
        {"subset_number":"885","subset_description 6":"hello 7"}
        [
    },
          ]
        }
        }

正如您所看到的,我基本上将所有子集信息组合在一起,以便响应更清晰并组合在一起。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

以下应该做你想做的事:

function reformat(input) {
    // We'll update this as we progress through the input array
    var output = {
        docs: []
    }

    // We use this as a handy, reusable way to either find an existing
    // setNumber, or create a placeholder for it and return the new one
    // if none existed
    function find(setNumber, setDescription) {
        var docs = output.docs;

        // Try and find an existing entry which has the same set number
        // as we're looking for
        for (var i=0;i<docs.length;i++) {
            if (docs[i]["set_number"] == setNumber) {
                return docs[i];
            };
        };

        // If we get this far, none exist; so create and add to the array here
        var newOne = {
            'set_number': setNumber,
            'set_description' : setDescription,
            subsets: []
        };

        docs.push(newOne);

        return newOne;
    };

    // Heres the actual work; loop through the input array, use `find` to
    // find or create the entry in the output array, and add the current
    // subset to its list
    for (var i=0;i<input.docs.length;i++) {
        var doc = input.docs[i];
        var current = find(doc["set_number"], doc["set_description"]);

        current.subsets.push({
            'subset_number': docs["subset_number"],
            'subset_description': docs["subset_description"]
        });
    }

    return output;
}

这样称呼:

var output = reformat(input); // where input should point to a variable storing the "response" property of the JSON you included.