[英]How to parse json response in jQuery mobile?
I am new to jquery mobile. 我是jQuery Mobile的新手。 I am trying to get contact name from contacts. 我正在尝试从联系人那里获取联系人姓名。 JSON by sending AJAX request. 通过发送AJAX请求进行JSON。 But I am not getting any alert when I click on submit button. 但是,当我单击“提交”按钮时,我没有收到任何警报。
My jQuery ajax request 我的jQuery ajax请求
$(document).ready(function() {
//after button is clicked we download the data
$("#submit").click(function(){
//start ajax request
$.ajax({
url: "myURL/contacts.json",
dataType: "json",
success: function(data) {
var json = $.parseJSON(data);
alert(json.name);
});
});
});
contacts.json Contacts.json
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
.
.
.
.
] } ]}
How to generate dynamic clickable list for above ajax response? 如何为上述ajax响应生成动态可点击列表?
from the looks of it, your json will be an array of object. 从它的外观来看,您的json将是一个对象数组。 Try doing json[0].Name to test it 尝试执行json [0] .Name进行测试
dataType: "json"
already specifies the returned data to be a json object. 已经将返回的数据指定为json对象。 so to read the values, you can simply use the object syntax: 因此,要读取值,您可以简单地使用对象语法:
success: function(data) {
//cycle trough returned "contacts":
for(var i=0;i<data.contacts.length;i++){
console.log(data.contacts[i].name);
}
}
Firstly our code is badly formated (smart quotes, wrong placement of parentheses). 首先,我们的代码格式错误(智能引号,括号的位置错误)。 Secondly since your contacts are in an array, you can't access them using json.name
, you should try something like this instead: 其次,由于您的联系人位于数组中,因此您无法使用json.name
访问,而应尝试如下操作:
$(document).ready(function () {
//after button is clicked we download the data
$("#submit").click(function () {
//start ajax request
$.ajax({
url: "myURL/contacts.json",
dataType: "json",
success: function (data) {
var json = $.parseJSON(data);
json.contacts.forEach(function(val, ind, arr){
alert(val.name);
});
}
});
});
});
your json data present inside contacts,so you should take name in this format. 您的json数据存在于联系人中,因此您应该以这种格式命名。
var json = $.parseJSON(data);
alert(json.contacts[0].name);
In stead of parsing JSON you can do like followng: 代替解析JSON,您可以执行以下操作:
$.ajax({
..
dataType: 'json' // using json, jquery will make parse for you
});
To access a property of your JSON do as shown in below: 要访问JSON的属性,请执行以下操作:
data[0].name;
data[0].email;
Why you need data[0] because data is an array, so to its content retrieve you need data[0] (first element), which gives you an object {"name":"myName" ,"address": "myAddress" }. 为什么需要data [0]因为数据是一个数组,所以要获取其内容,需要data [0](第一个元素),它为您提供了一个对象{“ name”:“ myName”,“ address”:“ myAddress” }。
And to access property of an object rule is: 而访问对象规则的属性是:
Object.property
or sometimes 有时
Object["property"] // in some case
So you need 所以你需要
data[0].name
and so on to get what you want. data[0].name
等,以获取所需的内容。
If you not 如果没有
set dataType: json
then you need to parse them using $.parseJSON()
and to retrieve data like above. 设置dataType: json
然后您需要使用$.parseJSON()
解析它们并检索如上的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.