如何从给定的代码中获取JSON对象
How to get the JSON object from the given code?
我正在使用一个url,我的代码是:
var url="http://nucleus/api/projectlist?format=json"
var json=JSON.parse(this.responseText);
浏览器中JSON格式的数据如下
{
"1": {
"id": "91",
"title": "Nucleus Aura",
"project_locations": "TVm, Kochi",
"project_type": "Villa",
"project_status": "Book Now",
"count_plan": 0,
"image": "uploads/project_images/projects_images_image1415954647.png",
"imagetitle": "Villa Night"
}
}
我想将JSON数据对象打印到一个警告框中。
将其分配给var
以获得一个Object
如果responseText为
{
"one": {
"id": "91",
"title": "Nucleus Aura",
"project_locations": "TVm, Kochi",
"project_type": "Villa",
"project_status": "Book Now",
"count_plan": 0,
"image": "uploads/project_images/projects_images_image1415954647.png",
"imagetitle": "Villa Night"
}
}
然后,
getJSON(url, function(json){
alert(json);// will give output like [object][Object]
alert(json.one.id);// will give output 91
}
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
var url = "http://nucleus/api/projectlist?format=json";
getJSON(url).then(function(data) {
alert('Your Json result is: ' + data.result); //you can comment this, i used it to debug
result.innerText = data.result; //display the result in an HTML element
}, function(status) { //error detection....
alert('Something went wrong.');
});
相关文章:
- 如何使用 Node JS 获取 JSON 嵌套数组值
- 使用Javascript/JQuery获取JSON GET数据
- 在jQuery中获取JSON
- 如何使用jQuery按键搜索和获取json行数据
- 如何使用jquery获取Json的节点
- 通过字符串获取JSON对象
- 通过Azure存储以HTML形式获取JSON文件
- 获取JSON并使用Javascript解析为字符串
- 如何在angular中获取json对象的名称
- 如何使用angular获取json对象的名称
- 如何使用Ajax JQuery.ech()获取JSON值
- 可以't通过从googlemapapi获取json值
- 获取json结果的身份验证问题
- Javascript:获取 JSON 中的值计数
- 按服务获取JSON数据
- 如何使用javascript获取json元素
- 从javascript文件中获取JSON,并使用NodeJS在路由文件中显示
- 如何从给定的代码中获取JSON对象
- 获取 JSON 数据将数组项添加到变量
- 如何从具有动态可观察属性的淘汰对象中获取 JSON 字符串