1

这是我用来读取 json 文件的 javascript 文件的一部分。

function initSearchInfo() {
    var tagContent = "";
    var tagsCount = 15;
    var i = 0;
    $.ajax({
            type : "GET",
        url : JSON_URL + SEARCH_HISTORY_JSON + EXT_JSON + versionParam,
        dataType : "json",
        contentType : "application/json",
        async : false,
        success : function(data) {
            $.each(data.count, function(key, val) {
                i++;
                if (i > tagsCount) {
                    return false;
                } else {
                    tagContent += "<li><a data-weight=" + val + "     href='"
                        + GLOBAL_SEARCH_URL + key + "'>" + key
                        + "</a></li>";
            }
        });
        $("#taglist").html(tagContent);

    },
    error : function(xhr, status, error) {
        $("#tagCloud").html(getMessage(tagcloud.error));
        $("#searchHistory").hide();
        console.log(status);
    }
});

}

我能够阅读这个 json 文件(1):

{
    "count": {
        "scm": {
            "count": 22,
            "date": "2013-05-08"
        },
        "java7": {
            "count": 22,
            "date": "2013-05-08"
        },
        "groovy": {
            "count": 22,
            "date": "2013-05-08"
        },
        "ldap": {
            "count": 21,
            "date": "2013-04-25"
        }
    },
    "date": "10Oct2013"
}

但是当我阅读这个文件(2)时:

{"count":"{\"ldap\":{\"count\":15,\"date\":\"2013-04-04\"},\"myplace\":{\"count\":12,\"date\":\"2013-05-08\"},\"ts-ws1\":{\"count\":11,\"date\":\"2013-05-08\"},\"hbase workshop\":{\"count\":11,\"date\":\"2013-05-08\"},"date":"11 Oct 2013"}

当我尝试读取文件 (2) 时,我的代码中断了。文件(2)是由gson库产生的。但文件(1)是我写的。

4

2 回答 2

0

file(2) 不是有效的 json 文件。第二个 { 之前不能有双引号,双引号之前也不能有反斜杠。请发布生成文件(2)的代码。

于 2014-07-15T21:07:45.897 回答
0

似乎存在语法错误,文件给出错误。“count”键的值在双引号中,并且它们没有正确结束。

于 2013-10-15T06:55:16.793 回答