簡體   English   中英

JSON解析錯誤意外令牌

[英]JSON parse error unexpected token

JSON數據就是這種格式的東西。

[{
    "_id": "5b706faf2605576fefb9ae9c",
    "index": 0,
    "guid": "46850469-5924-4966-b7d6-92125444cf98",
    "isActive": true,
    "balance": "$2,983.78",
    "friends": [{
            "fid": 0,
            "fname": "Sondra Fields"
        },
        {
            "fid": 1,
            "fname": "Sondra"
        },
        {
            "fid": 2,
            "fname": "Fields"
        }
    ],
    "greeting": "Hello,",
    "favoriteFruit": "apple"
}]

我試圖讓朋友“ fid”和“ fname”,但很難做到這一點。 我正在做這樣的事情。

var dat = { array: data }
var actual = dat.array.reduce((p, n, index) => p.concat(n[index - 1]))

最初,數據位於“數據”變量中。 通過這樣做console.log((actual.friends)); 我得到以下結果。

  [ { fid: 0, fname: 'Sondra Fields' },
  { fid: 1, fname: 'Sondra' },
  { fid: 2, fname: 'Fields' } ]

無法進一步提取數據。 謝謝

通過遍歷actual.friends ,可以得到fidfname的值。

 data=[ { "_id": "5b706faf2605576fefb9ae9c", "index": 0, "guid": "46850469-5924-4966-b7d6-92125444cf98", "isActive": true, "balance": "$2,983.78", "friends": [ { "fid": 0, "fname": "Sondra Fields" }, { "fid": 1, "fname": "Sondra" }, { "fid": 2, "fname": "Fields" } ], "greeting": "Hello,", "favoriteFruit": "apple" } ] var dat = { array: data } var actual = dat.array.reduce((p, n, index) => p.concat(n[index - 1])) actual.friends.forEach(function(element) { console.log(element.fid,element.fname); }); 

要么

data.forEach(function(ele) {
ele.friends.forEach(function(element) {
  console.log(element.fid,element.fname);
  });
});

 data=[ { "_id": "5b706faf2605576fefb9ae9c", "index": 0, "guid": "46850469-5924-4966-b7d6-92125444cf98", "isActive": true, "balance": "$2,983.78", "friends": [ { "fid": 0, "fname": "Sondra Fields" }, { "fid": 1, "fname": "Sondra" }, { "fid": 2, "fname": "Fields" } ], "greeting": "Hello,", "favoriteFruit": "apple" } ] data.forEach(function(ele) { ele.friends.forEach(function(element) { console.log(element.fid,element.fname); }); }); 

暫無
暫無

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

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