解析JSON输出问题
我打电话来获取基于国家/地区的语言。 。当它返回多国语言我下面的代码工作正常..但是当它返回只有一个记录的输出是不正确来临(即将为“indefined” ..请咨询解析JSON输出问题
这里是我的代码:
$.ajax({
type: 'POST',
url: "http://mymethod",
complete: function() { $.mobile.hidePageLoadingMsg(); },
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
"country": countryCode
}),
success: function (output, textStatus, jqXHR) {
$.each(output.geographyInfo, function (i, theItem) {
try {
languages.push("<option value='" + theItem.languageCode + "'>" + theItem.languageName + "</option>");
}
catch (error) {
alert(error);
}
});
$(languages.join('')).appendTo("#dpdLanguages").trigger("create");
}
});
这里是JSON输出
这里是输出时只有一种语言:
{ “geographyInfo”:{ “主动”: “真”, “COUNTRYCODE”: “美国”,“国家名称“:”UNITED STATES“,”instanceID“:”1“,”languageCode“:”EN“,”languageName“:”英语“,”regionNam E “:” 北美 “},” reasonDetails “:空,” 大小 “:” 1" , “状态”: “真正的”}
,并在多个案件
{ “geographyInfo”:[{ “主动”: “真”, “COUNTRYCODE”: “CA”, “国家名称”: “加拿大”, “实例ID”: “1”, “语言代码”: “EN”, “languageName”: “英语”,“regionName “:”北美“},{”active“:”true“,”countryCode“:”CA“,”countryName“:”CANADA“,”instanceID“:”1“,”languageCode“:”FR“ languageName“:”French“,”regionName“:”北美“},{”active“:”true“,”countryCode“:”CA“,”countryName“:”CANADA“,”instanceID“:”2“ “languageCode”:“EN”,“languageName”:“English”,“regionName”:“Americas”}, {“active”:“true”,“countryCode”:“CA”,“countryName” “实例ID”: “2”, “语言代码”: “FR”, “languageName”: “法语”, “regionName”: “美洲”}], “reasonDetails”:空, “尺寸”:“4”,“状态”:“真”}
是udidu,我认为多数民众赞成在这种情况..但如何解决这个问题?
最简单的解决方案在这里将是,如果你有只有一个geographyInfo元件将其包装在[]中,如下所示:
{"geographyInfo":[{"active":"true","countryCode":"US","countryName":"UNITED STATES","instanceID":"1","languageCode":"EN","languageName":"English","regionName":"North America"}],"reasonDetails":null,"size":"1","status":"true"}
因为这将充当一个元件阵列的,你是不需要改变你的代码。当前只有一个geographyInfo元素时,每个循环都会将内部geographyInfo项目显示为单独的数组元素,并且它将循环7次。
这里有一个工作示例:
的index.html:
<!DOCTYPE html>
<html>
<head>
<title>jQM Complex Demo</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script>
$(document).on('pagebeforeshow', '#index', function(){
$('#populate-button').on('click',function(e,data){
$('#dpdLanguages').empty()
$.ajax({
type: 'POST',
url: "json.php",
complete: function() { $.mobile.hidePageLoadingMsg(); },
dataType: "json",
contentType: 'application/json',
data: "",
success: function (output, textStatus, jqXHR) {
$.each(output.geographyInfo, function (i, theItem) {
try {
$('<option>').attr('value',theItem.languageCode).html(theItem.languageName).appendTo("#dpdLanguages");
}
catch (error) {
alert(error);
}
});
$('#dpdLanguages').selectmenu();
}
});
});
});
</script>
</head>
<body>
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
</div>
<div data-role="content">
<a href="#" data-role="button" id="populate-button">Load JSON data</a>
<select id="dpdLanguages"></select>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
jsop.php:
<?php
echo '{"geographyInfo":[{"active":"true","countryCode":"US","countryName":"UNITED STATES","instanceID":"1","languageCode":"EN","languageName":"English","regionName":"North America"}],"reasonDetails":null,"size":"1","status":"true"}';
?>
编辑:
如果你不能改变你的JSON代码,这将工作,以及:
$(document).on('pagebeforeshow', '#index', function(){
$('#populate-button').on('click',function(e,data){
$('#dpdLanguages').empty()
$.ajax({
type: 'POST',
url: "json.php",
complete: function() { $.mobile.hidePageLoadingMsg(); },
dataType: "json",
contentType: 'application/json',
data: "",
success: function (output, textStatus, jqXHR) {
if(typeof output.geographyInfo.length !== 'undefined') {
$.each(output.geographyInfo, function (i, theItem) {
try {
$('<option>').attr('value',theItem.languageCode).html(theItem.languageName).appendTo("#dpdLanguages");
}
catch (error) {
alert(error);
}
});
} else {
$('<option>').attr('value',output.geographyInfo['languageCode']).html(output.geographyInfo['languageName']).appendTo("#dpdLanguages");
}
$('#dpdLanguages').selectmenu();
}
});
});
});
谢谢Gajotres的回复。但我没有控制JSON输出...有没有解决方案从HTML/jQuery的一面? – 2013-02-24 14:31:15
看看我的答案,我添加了编辑部分。有一个如果成功,它将确定output.geographyInfo是一个数组还是单个元素。 – Gajotres 2013-02-24 14:53:04
嘿再次感谢你,只是一个问题什么是typeof variable_here?我正在使用jquery mobile ... – 2013-02-24 15:00:01
显示我们也是你的JSON输出,每一个案件。 – Gajotres 2013-02-24 09:37:57
听起来像当你接收多种语言时你得到的是一个对象数组,当它只有一种语言时,它是单个JSON对象 – udidu 2013-02-24 09:53:31
我用json输出编辑了这个问题 – 2013-02-24 10:41:16