簡體   English   中英

解析JSON問題

[英]Issue with parsing JSON

我已經提到了多個線程和教程,但仍然遇到這個問題!

我有兩個用於兩個不同功能的JSON響應:

1st(僅一個元素)

需要提取“ Intent_category”和“ count”

數據:

{ "_id" : { "SpId" : 664 , "Intention_category" : "" , "Report_Id" : 2 , "Channel_Id" : 4} , "count" : 1132}

功能:

function drawIntentionPieChart(data) {

        alert("In drawIntentionPieChart(...)");

        /* $.each(data[0], function(key, value) {
            alert("key : " + key + " value : " + value);
        }); */

        var jsonData = JSON.parse(data);

        alert("In drawIntentionPieChart(...) jsonData : " + jsonData
                + " jsonData[0] : " + jsonData[0]);
}

第二(兩個要素)

需要提取“ Emotion_category”和“ count”

數據:

{ "_id" : { "SpId" : 664 , "Emotion_category" : "joy" , "Report_Id" : 3 , "Channel_Id" : 4} , "count" : 7},{ "_id" : { "SpId" : 664 , "Emotion_category" : "neutral" , "Report_Id" : 3 , "Channel_Id" : 4} , "count" : 1125}

功能:

function drawEmotionPieChart(data) {

        /* $.each(data[0], function(key, value) {
            alert("key : " + key + " value : " + value);
        }); */

        var jsonData = JSON.parse(data);

        alert("In drawEmotionPieChart(...) jsonData : " + jsonData
                + " jsonData[0] : " + jsonData[0]);
}

在這兩種情況下,我都會收到以下警報:

jsonData:[對象對象] jsonData [0]:未定義

我什至嘗試使用JQuery(帶注釋的代碼),但鍵為0,值是整個數據,使用data [0]並編寫函數以警告鍵值也失敗。

我該如何進行?

請采取以下步驟解決您的問題:

var firstJsonStr = '{ "_id" : { "SpId" : 664 , "Intention_category" : "" , "Report_Id" : 2 , "Channel_Id" : 4} , "count" : 1132}';
var firstJson = JSON.parse(firstJsonStr);
alert(firstJson["_id"]["Intention_category"]);
alert(firstJson.count);

這意味着您只有一個對象而不是一個數組,如果希望它是一個數組,則必須用[]符號包裝。

第二個同樣的問題,同樣,您的json字符串缺少[]部分,如您所見,我已經添加到json了:

var secondJsonStr = '[{ "_id" : { "SpId" : 664 , "Emotion_category" : "joy" , "Report_Id" : 3 , "Channel_Id" : 4} , "count" : 7},{ "_id" : { "SpId" : 664 , "Emotion_category" : "neutral" , "Report_Id" : 3 , "Channel_Id" : 4} , "count" : 1125}]';

var secondJson = JSON.parse(secondJsonStr);
alert([secondJson[0].count, secondJson[1].count]);
alert([secondJson[0]["_id"].Emotion_category, secondJson[1]["_id"].Emotion_category]);

如何解決? 看來您是從后端獲取此數據的,並且如果后端未將對象包裝在[] ,則意味着服務器端JSONParser API出了問題,這里的另一個可能是,您僅在您的代碼中解析json字符串而無需使用任何API。 如果是,只需將輸出包裝在數組符號中即可解決問題,如果不更改,請更改API。

在這兩種情況下,您的json均無效。 您可以簡單地通過將數據放入jsonlint進行測試 如果您解析它,則會得到以下信息:

{
    "_id": {
        "SpId": 664,
        "Emotion_category": "joy",
        "Report_Id": 3,
        "Channel_Id": 4
    },
    "count": 7
},
{
    "_id": {
        "SpId": 664,
        "Emotion_category": "neutral",
        "Report_Id": 3,
        "Channel_Id": 4
    },
    "count": 1125
}

它有2個“根”,應該引發解析錯誤。 您需要在開頭添加[ ,在末尾添加]以使其有效json。

更正了json格式的字符串:

var jsonstring = '[{"_id":{"SpId":664,"Emotion_category":"joy","Report_Id":3,"Channel_Id":4},"count":7},{"_id":{"SpId":664,"Emotion_category":"neutral","Report_Id":3,"Channel_Id":4},"count":1125}]'

嘗試這個,

function drawEmotionPieChart(data) {

        /* $.each(data[0], function(key, value) {
            alert("key : " + key + " value : " + value);
        }); */

        var jsonData = JSON.parse(data);
console.log(jsonData)
        alert("In drawEmotionPieChart(...) jsonData : " + jsonData
                + " jsonData[0] : " + jsonData['_id']['Intention_category']);
}
data = '{ "_id" : { "SpId" : 664 , "Intention_category" : "like" , "Report_Id" : 2 , "Channel_Id" : 4} , "count" : 1132}'
drawEmotionPieChart(data)

演示鏈接http://jsfiddle.net/mdAdH/

暫無
暫無

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

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