SyntaxError:JSON.parse:JSON数据第1行第2列的预期属性名称或“}”

时间:2016-03-22 11:07:35

标签: json ajax json.net

我试图通过weservice发送json,但它在ajax调用中给出了一个错误,我称之为web服务。

我创建json的代码是:

sb.Append('[');          
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            switch (ds.Tables[0].Rows[i]["transferTo"].ToString())
            {
                case "Individual":
                    {                           
                        sb.Append("{pathImage:\"individual.png\",");
                        break;
                    }
                case "Stock":
                    {
                        sb.Append("{pathImage:\"stock.png\",");
                        break;
                    }

                case "Store":
                    {
                        sb.Append("{pathImage:\"store.png\",");
                        break;
                    }
                case "Inuse":
                    {
                        sb.Append("{pathImage:\"inuse.png\",");
                        break;
                    }
                case "Repair":
                    {
                        sb.Append("{pathImage:\"repair.png\",");
                        break;
                    }                                     
            }
           sb.Append("patharrow :\"arrow.png\"").Append("}");
           if (i != ds.Tables[0].Rows.Count-1) { sb.Append(','); }      
        }
        return sb.Append(']').ToString();

代码有什么问题?

输出是这样的:

{
    [
      {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "stock.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "store.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "repair.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "inuse.png",
        patharrow: "arrow.png"
    }, {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }, 
      {
        pathImage: "individual.png",
        patharrow: "arrow.png"
    }
    ]
}

我要做的是在我的ajax调用中调用上面的webservice代码:

$.ajax(
        {
            type: "POST",
            url: "assetHistory.aspx/getImagesPath",
            data: JSON.stringify(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (data)
            {
                //Check length of returned data, if it is less than 0 it means there is some status available
                if (data.d != "failure")
                {
                    json = $.parseJSON(data.d);
                    var output = "<ul>";
                    alert("from json :" + json);
                    //wish to iterate here through each pathImage to store here
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

您不应为每个属性附加{

替换

sb.Append("{pathImage:\"individual.png\",");

通过

sb.Append("pathImage:\"individual.png\",");

修改

根据OP的新信息,我认为第一个属性的关键是错过了

由于您还没有共享首先生成的代码部分[所以我将向您解释为什么会出现此错误 - 您的JSON在第一个数组值之前缺少密钥。那是{[]}无效的。它应该是{d:[]}。