gpt4 book ai didi

javascript - 使用 JavaScript 从服务器响应创建 JSON

转载 作者:行者123 更新时间:2023-12-01 01:44:44 26 4
gpt4 key购买 nike

我从远程服务器收到此响应。

A has 100 products\n   Brought: \n      ID/234 has brought 8 products\n      ID/212 has brought 72 products\n   Not Brought\n\n B has 0 products\n   Brought\n   Not Brought\n

When I execute my node js script on the terminal this response is displayed as a table as shown in the screenshot below.

enter image description here

I want this output as JSON.

Here is my code.

function(err, res) {
product=res.content; //store the response in variable
var prodArr = [];
var obj = product.split('\n'); //split the response at new lines
for(var i= 1; i<obj.length; i=i+1)
{
prodArr.push({
data : obj[i]
});

}
console.log(prodArr);
})
}

这是我执行上述脚本时得到的 JSON。

{ data:'A has 100 products' },
{ data: 'Brought: '},
{ data:'ID/234 has brought 8 products ' },
{ data:'ID/212 has brought 72 products ' },
{ data: 'Not Brought' },
{ data:'B has 0 products' },
{ data: 'Brought: '},
{ data: 'Not Brought '},

但是我想要 JSON,应该如下所示:

{
"data":{
"title": "A has 100 products",
"Brought": {
"1" : "ID/234 has brought 8 products",
"2" : "ID/212 has brought 72 products"
},
"Not Brought" : {
}
}
},

{
"data":{
"title": "B has 0 products",
"Brought": {
},
"Not Brought" : {
}
}
}

我该怎么做?

最佳答案

您需要使用正则表达式来确定标题元素的位置以及带和不带的项目,因为解决方案是完全具体的。

var input = "A has 100 products\n   Brought: \n      ID/234 has brought 8 products\n      ID/212 has brought 72 products\n   Not Brought\n\n B has 0 products\n   Brought\n   Not Brought\n";
var splitInput = input.split('\n'); // split with newline
var formattedInput = splitInput.map(slice => slice.trim()); // trim extra spaces
var titleIndices = [], broughtIndices = [], notBroughtIndices = []; // figure out necessary indices in string
for(var i = 0; i < formattedInput.length; i++) {
if(/\w+ has \d+ products/.test(formattedInput[i])){
titleIndices.push(i);
}
if(/^Brought\:?/.test(formattedInput[i])) {
broughtIndices.push(i);
}
if(/^Not Brought\:?/.test(formattedInput[i])) {
notBroughtIndices.push(i);
}
}
const output = [];
for(var i = 0; i < titleIndices.length; i++) {
const broughtLength = notBroughtIndices[i] - broughtIndices[i] - 1;
let brought = {};
for(var j=0; j < broughtLength; j++) {
const broughtItem = formattedInput[broughtIndices[i]+1+j];
if(broughtItem) {
brought[j+1] = broughtItem;
}
}
const notBroughtLength = (titleIndices[i+1] || notBroughtIndices[i] ) - notBroughtIndices[i] - 1;
let notBrought = {};
for(var j=0; j < notBroughtLength; j++) {
const notBroughtItem = formattedInput[notBroughtIndices[i]+1+j]
if (notBroughtItem) {
notBrought[j+1] = notBroughtItem;
}
}
output.push({
data: {
title: formattedInput[titleIndices[i]],
Brought: brought,
"Not Brought": notBrought,
}
});
}
console.log(JSON.stringify(output))

关于javascript - 使用 JavaScript 从服务器响应创建 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52054889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com