简体   繁体   English

无法解析JSON字符串

[英]Unable to parse the JSON string

I have the JSON file input.json: 我有JSON文件input.json:

{
"consumner_key": {
    "display_name": "CONSUMER-KEY:",
    "name":"consumer_key",
    "format": "string",
    "type": "textbox",
    "isMandatory": "true"
},
"secret_key": {
    "display_name": "CONSUMER-SECRET:",
    "name":"consumer_secret",
    "format": "string",
    "type": "textbox",
    "isMandatory": "true"
}
}

I am using $.getJSON() to get the JSON file and parse it: 我使用$.getJSON()来获取JSON文件并解析它:

$.getJSON('input.json',function jsonData(Data)
{
$.each(Data, function(m,field) 
        {
            console.log(m);
            $('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.consumner_key+''+this.name});
            $('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.secret_key+''+this.name});
        });

});

When I run it I am not able to view the input boxes in my tabs. 当我运行它时,我无法查看选项卡中的输入框。 Kindly point to where I am going wrong. 请指出我出错的地方。

you say you have file input.js , but in the code you have input.json - can that be a reason? 你说你有文件input.js ,但在代码中你有input.json - 这可能是一个原因吗?

UPDATE UPDATE

You also give name to your function - jsonData . 您还为您的函数命名 - jsonData As far as I know you should not name inline javascript functions, it will not compile. 据我所知你不应该命名内联javascript函数,它不会编译。 Just do 做就是了

$.getJSON('input.json',function (Data) {
    $.each(Data, function(m,field) {
        console.log(m);
        $('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.consumner_key+''+this.name});
        $('#tabs-4 social').append('<input></input>').attr({type:'text',name:this.secret_key+''+this.name});
    });

});

UPDATE 2 更新2

Furthermore, you do this.consumner_key+''+this.name and this.secret_key+''+this.name , while consumner_key and name are properties of different levels. 此外,你执行this.consumner_key+''+this.namethis.secret_key+''+this.name ,而consumner_keyname是不同级别的属性。 I believe, this inside each should represent each single sub-object inside your json object. 我相信, this里面each应该代表你的JSON对象内的每个单独的子对象。 So it will have property name , but not consumner_key . 所以它将有属性name ,但不是consumner_key I may be wrong, but anyway this cannot have both properties. 我可能错了,但无论如何this不能兼具两个属性。

I finally managed to clear all the errors.Specifying MIME type in web.xml of tomcat was not enough.I had to manually override it in my js file.The below code removed the error "not well formed" 我终于设法清除了所有错误。在tomcat的web.xml中指定MIME类型是不够的。我不得不在我的js文件中手动覆盖它。下面的代码删除了错误“not well formed”

$.ajaxSetup({beforeSend: function(xhr){   
if (xhr.overrideMimeType)  
{     
    xhr.overrideMimeType("application/json");}
} 
});

Also I was refering the json files both in my jsp and javascript file which was causing the invalid label errors 另外,我在jsp和javascript文件中引用了json文件,这导致了无效的标签错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM