[英]Unable to parse the JSON string
我有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"
}
}
我使用$.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});
});
});
當我運行它時,我無法查看選項卡中的輸入框。 請指出我出錯的地方。
你說你有文件input.js
,但在代碼中你有input.json
- 這可能是一個原因嗎?
UPDATE
您還為您的函數命名 - jsonData
。 據我所知你不應該命名內聯javascript函數,它不會編譯。 做就是了
$.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});
});
});
更新2
此外,你執行this.consumner_key+''+this.name
和this.secret_key+''+this.name
,而consumner_key
和name
是不同級別的屬性。 我相信, this
里面each
應該代表你的JSON對象內的每個單獨的子對象。 所以它將有屬性name
,但不是consumner_key
。 我可能錯了,但無論如何this
不能兼具兩個屬性。
我終於設法清除了所有錯誤。在tomcat的web.xml中指定MIME類型是不夠的。我不得不在我的js文件中手動覆蓋它。下面的代碼刪除了錯誤“not well formed”
$.ajaxSetup({beforeSend: function(xhr){
if (xhr.overrideMimeType)
{
xhr.overrideMimeType("application/json");}
}
});
另外,我在jsp和javascript文件中引用了json文件,這導致了無效的標簽錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.