1

实际上我想通过 jQuery mobile 解析一个 json 数组,但是我被卡住的地方没有被搜索很多,请帮助我。

我的json格式

{
"messagedetails":
    [{
    "msgData":"DEAR MEMBER\\\"S TODAY\\\"S KITTY WINNER IS N0 55.HOPE YOU ALL ENJOYED THE EVENING. THANK YOU\r\n MAHAVIRD (PLEASE KEEP SLIPS CAREFULLY)",
    "requestNosSms":64,
    "requestUserid":"16459",
    "spamReason":
    {
        "userApprove":"0",
        "senderApprove":"0",
        "templateMatch":80,
        "templateId":"6586",
        "spamKeywords":["winner"],
        "maxNosLimit":"0"
    },
    "id":"5124c1c446923e6c4100005f",
    "userName":"MAHAVIR DIAMOND",
    "date":"2013-02-20 17:59:57"
    },

     {
    "msgData":"DEAR MEMBER\\\"S TODAY\\\"S KITTY WINNER IS N0 55.HOPE YOU ALL ENJOYED THE EVENING. THANK YOU\r\n MAHAVIRD (PLEASE KEEP SLIPS CAREFULLY)",
    "requestNosSms":64,
    "requestUserid":"16459",
    "spamReason":
    {
        "userApprove":"0",
        "senderApprove":"0",
        "templateMatch":80,
        "templateId":"6586",
        "spamKeywords":["winner"],
        "maxNosLimit":"0"
    },
    "id":"5124c1c446923e6c4100005f",
    "userName":"MAHAVIR DIAMOND",
    "date":"2013-02-20 17:59:57"
    }]
}

我正在使用这样的代码

var test =null;                                     
                                        $.each(data, function(i,messages) {
                                            test = test + "<h1>"+messages][2]+"</h1>";

});

$("#results").html(test);
4

2 回答 2

1

在 json 中你不能在值中使用引号"你必须对值进行编码

输入

 array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

通过某种编码输出

 Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]
于 2013-02-20T13:03:05.920 回答
0

您的 JSON 是有效的。

只需尝试使用此代码:

var test =null;                                     
$.each(data.messagedetails, function(i,messages) {
test = test + "<h1>" + messages.msgData +"</h1>";
});
$("#results").html(test);
于 2013-02-20T15:15:25.827 回答