Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 完全重新格式化json响应_Jquery_Json - Fatal编程技术网

Jquery 完全重新格式化json响应

Jquery 完全重新格式化json响应,jquery,json,Jquery,Json,我有一个.json响应,需要将其重新格式化为不同的响应,最终我将重新分析并保存该响应,以便在其他地方的代码的不同部分中使用 我不完全确定如何描述我在这里所做的工作,但在我知道我的数组以正确的格式分组后,我可以解析json:-\ 请允许我向您展示代码,以及输出应该是什么样子: {"response": {"docs":[ { "set_number":"1","set_description":"first description", "subsets

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

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

请允许我向您展示代码,以及输出应该是什么样子:

{"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"} [ }, ] } } 当前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" }, ] } } {“答复”: {“文件”:[ {“集合描述号”:“1”,“集合描述号”:“第一描述”,“子集描述号”:“835”,“子集描述号1”:“你好2”}, {“集合描述编号”:“1”,“集合描述”:“第一描述”,“子集描述编号”:“845”,“子集描述2”:“你好3”}, {“集合描述编号”:“2”,“集合描述”:“第一描述”,“子集描述编号”:“855”,“子集描述3”:“你好4”}, {“集合描述编号”:“2”,“集合描述”:“第一描述”,“子集描述编号”:“865”,“子集描述4”:“hello 5”}, {“集合号”:“3”,“集合号说明”:“第一个说明”,“子集号”:“875”,“子集号说明5”:“你好6”}, {“集合描述编号”:“4”,“集合描述”:“第一描述”,“子集描述编号”:“885”,“子集描述6”:“你好7”}, {“集合编号”:“4”,“集合描述”:“第一描述”,“子集编号”:“895”,“子集描述7”:“你好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”,“集合描述”:“第一描述”, “子集”:[ {“子集编号”:“845”,“子集描述2”:“hello 3”}, {“子集编号”:“835”,“子集描述1”:“你好2”} ] }, { “集合编号”:“2”,“集合描述”:“第一描述”, “子集”:[ {“子集编号”:“855”,“子集描述3”:“hello 4”}, {“子集编号”:“865”,“子集描述4”:“hello 5”} }, { “集合编号”:“3”,“集合描述”:“第一描述”, “子集”:[ {“子集编号”:“875”,“子集描述5”:“你好6”} ] }, { “集合编号”:“4”,“集合描述”:“第一描述”, “子集”:[ {“子集编号”:“895”,“子集描述7”:“hello 8”}, {“子集编号”:“885”,“子集描述6”:“你好7”} [ }, ] } } 如您所见,我基本上组合了所有子集信息,因此响应更清晰,并分组在一起


非常感谢您提供的任何帮助!

以下内容可以满足您的要求:

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;
}

你想用JavaScript还是怎么做?