通过Google脚本解析嵌套的JSON

时间:2018-06-17 13:56:48

标签: json parsing google-apps-script google-sheets-api jsonparser

我一直在努力尝试在线找到成功解析API请求的JSON输出的方法。

我正在寻找解析以下API JSON示例输出的“ResultData”部分:

{ “URL”: “http://elasticbeanstalk.com/api/results/69/”, “标识”:69, “用户”:3 “inputdata”:{ “Q1”: “响应1”, “Q2”: “RESPONSE2”,” Q3 “:” Response3" , “Q4”: “Response4”, “Q5”: “Response5”}, “ResultData”: “{\” 指数\ “:1551,\” ID \ “:5246,\” ATTRIBUTE1 \ “:\” 输出1 \ “\ ”Attribute2 \“:\ ”输出2“,\ ”Attribute3 \“:16,\ ”Attribute4 \“:62.9433099,\ ”Attribute5 \“:\” www.google.com.au \}} \ n {\“index \”:1551,\“ID \”:5246,\“Attribute1 \”:\“Output1 \”,\“Attribute2 \”:\“Output2”,\“Attribute3 \” :16,\“Attribute4 \”:62.9433099,\“Attribute5 \”:\“www.google.com.au \”} \ n {\“index \”:1551,\“ID \”:5246,\“ ATTRIBUTE1 \ “:\” 输出1 \ “\ ”Attribute2 \“:\ ”输出2“,\ ”Attribute3 \“:16 \ ”Attribute4 \“:62.9433099,\ ”Attribute5 \“:\” www.google.com .au \“} \ n {\”index \“:1551,\”ID \“:5246,\”Attribute1 \“:\”Output1 \“,\”Attribute2 \“:\”Output2“,\”Attribute3 \“:16,\”Attribute4 \“:62.9433099,\”Attribute5 \“:\”www.google.com.au \“} \ n {\”index \“:1551,\”ID \“:5246, \ “ATTRIBUTE1 \”:\ “输出1 \”,\ “Attribute2 \”:\ “输出2”,\ “Attribute3 \”:16,\ “Attribute4 \”:62.9433099,\ “Attribute5 \”:\“www.google .com.au \“} \ n {\”index \“:1551,\”ID \“:5246 ,\ “ATTRIBUTE1 \”:\ “输出1 \”,\ “Attribute2 \”:\ “输出2”,\ “Attribute3 \”:16,\ “Attribute4 \”:62.9433099,\ “Attribute5 \”:\“万维网。 google.com.au \“} \ n {\”index \“:1551,\”ID \“:5246,\”Attribute1 \“:\”Output1 \“,\”Attribute2 \“:\”Output2“, \“Attribute3 \”:16,\“Attribute4 \”:62.9433099,\“Attribute5 \”:\“www.google.com.au \”} \ n {\“index \”:1551,\“ID \” :5246,\ “ATTRIBUTE1 \”:\ “输出1 \”,\ “Attribute2 \”:\ “输出2”,\ “Attribute3 \”:16,\ “Attribute4 \”:62.9433099,\ “Attribute5 \”:\” www.google.com.au \“} \ n {\”index \“:1551,\”ID \“:5246,\”Attribute1 \“:\”Output1 \“,\”Attribute2 \“:\”Output2 “\ ”Attribute3 \“:16,\ ”Attribute4 \“:62.9433099,\ ”Attribute5 \“:\ ”www.google.com.au \“}”}

要为导出到Google表格的每一行输出以下值:

索引ID Attribute1 Attribute2 Attribute3 Attribute4 Attribute5

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

2 个答案:

答案 0 :(得分:0)

使用String.split()分隔“ResultData”字符串,并使用换行符(\ n)作为分隔符,然后JSON.parse()生成数组的元素。

答案 1 :(得分:0)

将其解析,然后拆分,然后使用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)
    }

}