Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
通过Google脚本解析嵌套JSON_Json_Parsing_Google Apps Script_Google Sheets Api_Jsonparser - Fatal编程技术网

通过Google脚本解析嵌套JSON

通过Google脚本解析嵌套JSON,json,parsing,google-apps-script,google-sheets-api,jsonparser,Json,Parsing,Google Apps Script,Google Sheets Api,Jsonparser,我一直在努力寻找一种在线方式来成功解析来自API请求的JSON输出 我希望解析以下API JSON示例输出的“ResultData”部分: {“url”:“id”:69,“用户”:3,“输入数据”:{“Q1”:“Response1”,“Q2”:“Response2”,“Q3”:“Response3”,“Q4”:“Response4”,“Q5”:“Response5”},“ResultData”:“{“index\”:1551,“id\”:5246,“Attribute1\”:“Output1\”

我一直在努力寻找一种在线方式来成功解析来自API请求的JSON输出

我希望解析以下API JSON示例输出的“ResultData”部分:

{“url”:“id”:69,“用户”:3,“输入数据”:{“Q1”:“Response1”,“Q2”:“Response2”,“Q3”:“Response3”,“Q4”:“Response4”,“Q5”:“Response5”},“ResultData”:“{“index\”:1551,“id\”:5246,“Attribute1\”:“Output1\”,“Attribute2\”:“Output2\”,“Attribute3\”:16,“Attribute4\”,“Attribute4\”:62.9433099,\:“E5\”:“www.google.com:“Attribute1\”:“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”,“Attribute1\”\“输出1 1\”,“归因2\”:“输出2\”,,“归因3\”:16,,“归因4\”:62.94339 9 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9,”,”归因5 10 10 10 10 10 10 10 10 10 10 10 10 10 9 9 9 9 9,”,”,”,”,”归因5 9 9 9 9 9 9 9 9,”,”,”,”,”,”归因5 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”归因5 9 9 9 9 9 9,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”,”归因5 10 10 10 10 10 10 10 10 10 10 10 10 10 E1\“:\“输出1\”\“归因7.9 9 9,”,”归因5.9 9 9,”,”,”,”归因5.9 9 9,”,”,”,”归因5.10 10 10 10 10,”,”,”,”,”归因5.9 9”以下以下以下::“www.google.com.com.com.au\n{“索引”倒倒倒倒10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10,”,”,”,”,”,”,”,”,”互联网谷歌网站网站网站www.google.谷歌.com.com.com.com.com.com.com.com.com.com.com.com...金金金金金金属金属金属金属金属金属金属金属金属金属金属,\“属性2\”:\“目前,6.94339.9,“归因5\”:::“www.google.google.com.com.au\”:“www.google.com.com.au\”\n\n{索引:1551,,“ID\,,,,,“归因3”2“,,,,,,,,,“归因2”2.2.2月2.2,,,,,,,,,,,,“归因5\“归因5\”:::::“www.google.google.谷歌.com.com.com.com.com.com.com.com.com.com.com.com.com.com.com.10 10 10 10 10 10 10 10 10,,,,,,{{{{{{倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒倒\“输出2”,\”Attribute3\“:16,\“Attribute4\”:62.9433099,\“Attribute5\”:“www.google.com.au\”}\n{“索引”:1551,\“ID\”:5246,\“Attribute1\”:“Output1\”,“Attribute2\”:“Output2”,“Attribute3\”:16,\“Attribute4\”:62.9433099,\“Attribute5\:“www.google.com.au\”}

希望将每行的以下值导出到google工作表中:

索引ID属性1属性2属性3属性4属性5


有什么想法吗?任何正确方向的帮助或指点都将不胜感激

只需使用换行符(\n)作为分隔符拆分“ResultData”字符串,然后拆分结果数组的元素。

解析它,然后拆分它,然后使用parseJSON函数在各个结果数据行中循环

var OutputData = JSON.parse(response).ResultData;
OutputData = OutputData.split('\n');

// run this look while the variable i is less than 5 and increment i by 1 each time 
the end of the loop is reached
for(var i = 0; i < 5; i++) {
var ResultData = JSON.parse(OutputData[i]);
parseJSON(i,ResultData);
}

}

function parseJSON(result, myObject) {
// define an array of all the object keys

var headerRow = Object.keys(myObject);

// define an array of all the object values
var row = headerRow.map(function(key){ return myObject[key]});

// define the contents of the range
var contents = [
 headerRow,
 row
];

var contents1 = [
 row
];

// select the range and set its values
var ss = SpreadsheetApp.getActive();
var rng = ss.getSheetByName("Results").getRange(1, 1, contents.length, headerRow.length )
var rng1 = ss.getSheetByName("Results").getRange(result+2, 1, 1, headerRow.length )

   if (result == 0 ) {
        rng.setValues(contents)
    }
    else {
        rng1.setValues(contents1)
    }

}
var OutputData=JSON.parse(response).ResultData;
OutputData=OutputData.split('\n');
//在变量i小于5且每次递增i 1时运行此外观
到达循环的末尾
对于(变量i=0;i<5;i++){
var ResultData=JSON.parse(OutputData[i]);
parseJSON(i,ResultData);
}
}
函数parseJSON(结果,myObject){
//定义所有对象键的数组
var headerRow=Object.keys(myObject);
//定义所有对象值的数组
var row=headerRow.map(函数(键){return myObject[key]});
//定义范围的内容
变量内容=[
海德罗,
一行
];
变量contents1=[
一行
];
//选择范围并设置其值
var ss=SpreadsheetApp.getActive();
var rng=ss.getSheetByName(“结果”).getRange(1,1,contents.length,headerRow.length)
var rng1=ss.getSheetByName(“结果”).getRange(结果+2,1,1,headerRow.length)
如果(结果==0){
rng.setValues(目录)
}
否则{
rng1.setValues(内容1)
}
}

@prks21很高兴我能帮忙。但是,代码中有一些冗余。你应该考虑优化它。