无法使用Jquery mobile解析json

时间:2013-02-20 13:00:56

标签: jquery json jquery-mobile

实际上我想通过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);

2 个答案:

答案 0 :(得分: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","é"]

答案 1 :(得分:0)

您的JSON有效。

尝试使用此代码:

var test =null;                                     
$.each(data.messagedetails, function(i,messages) {
test = test + "<h1>" + messages.msgData +"</h1>";
});
$("#results").html(test);