0

我有从服务返回的这个 Json 数据。

这是我完整的 json 数据

 d: "[{"ImagePath":null,"ThemeTemplateId":1,"BorderWidth":null,"BorderStyle":null,"OptionTextUnderline":null,"OptionTextItalic":null,"OptionTextBold":null,"OptionTextSize":null,"OptionTextFont":null,"QuestionTextUnderline":null,"QuestionTextItalic":null,"QuestionTextBold":null,"QuestionTextSize":null,"QuestionTextFont":null,"SurveyTitleUnderline":null,"SurveyTitleItalic":null,"SurveyTitleBold":null,"SurveyTitleSize":null,"SurveyTitleFont":null,"BorderColor":null,"SurveyTitleColor":null,"OptionTextColor":null,"ThemeName":null,"BackgroundColor":null,"QuestionTextColor":null},{"ImagePath":null,"ThemeTemplateId":2,"BorderWidth":null,"BorderStyle":null,"OptionTextUnderline":null,"OptionTextItalic":null,"OptionTextBold":null,"OptionTextSize":null,"OptionTextFont":null,"QuestionTextUnderline":null,"QuestionTextItalic":null,"QuestionTextBold":null,"QuestionTextSize":null,"QuestionTextFont":null,"SurveyTitleUnderline":null,"SurveyTitleItalic":null,"SurveyTitleBold":null,"SurveyTitleSize":null,"SurveyTitleFont":null,"BorderColor":null,"SurveyTitleColor":null,"OptionTextColor":null,"ThemeName":null,"BackgroundColor":null,"QuestionTextColor":null}]"

///ajax函数

   jQuery.ajax({
        type: "POST",
        url: "3.aspx/GetThemeList",
        data: "{'clientid':'" + -1 + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (returndata) {
            console.log(returndata);
            jQuery.each(returndata, function (index, theme) {
                alert(theme.ImagePath);
                alert(theme.ThemeTemplateId);
            });
        }
    });

但它对我不起作用是否有任何其他方法可以通过 jquery 读取这些数据。

谢谢你的帮助。

4

4 回答 4

1

我认为它的工作http://jsfiddle.net/SrsxA/2/ ...

但是在您的帖子中,您错过了,第二个 JSON 对象的逗号()

 [{"ImagePath":null,"ThemeTemplateId":1},{"ImagePath":null, "ThemeTemplateId":2}]

                                                           ^-- need comma here

根据编辑

d: "[{"Ima..

   ^--- you don't need `"` here and at last, don't need to wrap with `""`.

完整的工作代码:

var themelist = {
    d: [{
        "ImagePath": 'a',
        "ThemeTemplateId": 1,
        "BorderWidth": null,
        "BorderStyle": null,
        "OptionTextUnderline": null,
        "OptionTextItalic": null,
        "OptionTextBold": null,
        "OptionTextSize": null,
        "OptionTextFont": null,
        "QuestionTextUnderline": null,
        "QuestionTextItalic": null,
        "QuestionTextBold": null,
        "QuestionTextSize": null,
        "QuestionTextFont": null,
        "SurveyTitleUnderline": null,
        "SurveyTitleItalic": null,
        "SurveyTitleBold": null,
        "SurveyTitleSize": null,
        "SurveyTitleFont": null,
        "BorderColor": null,
        "SurveyTitleColor": null,
        "OptionTextColor": null,
        "ThemeName": null,
        "BackgroundColor": null,
        "QuestionTextColor": null},
    {
        "ImagePath": 'b',
        "ThemeTemplateId": 2,
        "BorderWidth": null,
        "BorderStyle": null,
        "OptionTextUnderline": null,
        "OptionTextItalic": null,
        "OptionTextBold": null,
        "OptionTextSize": null,
        "OptionTextFont": null,
        "QuestionTextUnderline": null,
        "QuestionTextItalic": null,
        "QuestionTextBold": null,
        "QuestionTextSize": null,
        "QuestionTextFont": null,
        "SurveyTitleUnderline": null,
        "SurveyTitleItalic": null,
        "SurveyTitleBold": null,
        "SurveyTitleSize": null,
        "SurveyTitleFont": null,
        "BorderColor": null,
        "SurveyTitleColor": null,
        "OptionTextColor": null,
        "ThemeName": null,
        "BackgroundColor": null,
        "QuestionTextColor": null}]
};

$.each(themelist.d, function(index, theme) {
    console.log(theme['ImagePath']);
    console.log(theme.ImagePath);
});
于 2012-06-11T07:54:16.970 回答
0

尝试这个:

success: function (returndata) {

    // objectify it, if not already
    returndata = (typeof returndata == 'string') ? JSON.parse(returndata) : returndata;

    var themelist = returndata.d;

    .... // rest of your code

}
于 2012-06-11T07:57:03.193 回答
0

这真的是 JSON 字符串的编码方式吗?似乎您用引号打开了字符串:"而不是撇号'

于 2012-06-11T09:45:01.967 回答
0

我通常使用此代码,工作得很好。我设置了一个带有布尔检查$data["result"] = falsejson值的数据数组。如果为真则返回json object $data["arrayfrombackendcode"]

success: function(data, textStatus, jqXHR) {
     for(var i = 0, var; var= data.arrayfrombackendcode[i]; i++) {
         data.id
         data.name
         etc
     }
},

希望答案有帮助

于 2012-06-11T08:04:38.900 回答