簡體   English   中英

使用JavaScript從服務器響應中創建JSON

[英]Create a JSON from a servers response using JavaScript

我從遠程服務器收到此響應。

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

當我在終端上執行node js腳本時,此響應顯示為表格,如下面的屏幕快照所示。

在此處輸入圖片說明

我希望此輸出為JSON。

這是我的代碼。

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" : {
    }
 }
}

我怎么做?

由於解決方案是完全特定的,因此您需要使用正則表達式來確定title元素以及已帶和不帶項目的位置。

 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)) 

 var InputValue=['A has 100 Product','Brought','ID/1251 brought 8 Product','ID/1252 brought 4 Product','Not Brought','B has 100 Product','Brougth','ID/1222 brought 8 Product','ID/1333 brought 2 Product','Not Brought'] var prodArr={}; var finalResult=[]; var BroughtArr=[] let i=0; for(i=0;i<InputValue.length;i++){ if(i==0){ prodArr['Title']=InputValue[0]; i++; } if(InputValue[i]==='Brought'){ ++i; for(let j=i;j<InputValue.length;j++){ if(InputValue[j]!='Not Brought'){ BroughtArr.push(InputValue[j]); }else{ prodArr['Brought']=BroughtArr finalResult.push(prodArr); BroughtArr=[] prodArr={} if(i<InputValue.length) prodArr['Title']=InputValue[++j]; i=++j; } } } } console.log(finalResult) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM