我有一个通过php输出json数据的代理脚本,我希望能够使用javascript操作这些数据.我有以下代码,但它只获取php脚本输出的整个json字符串.如何获取数据并能够使用此json数据访问各个对象?
var xmlhttp;
function loadXMLDoc(url, cfunc) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = cfunc;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
loadXMLDoc("http://xxxxx.appspot.com/userbase_us.php?callback=userdata", function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var json = xmlhttp.responseText;
alert(json);
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下json数据获取JSON:
{
"count": 96,
"value": {
"title": "cotripWeatherStations",
"description": "Pipes Output",
"link": "http:\/\/pipes.yahoo.com\/pipes\/pipe.info?_id=f43ccc14ab9d1c88d537236e93c1525e",
"pubDate": "Mon, 03 Sep 2012 02:17:48 +0000",
"generator": "http:\/\/pipes.yahoo.com\/pipes\/",
"callback": "",
"items": [
{
"ws:Location": {
"global:Latitude": "37.086807",
"global:Longitude": "-104.521294"
},
"ws:Device": {
"global:DeviceId": "6796",
"global:CommonName": "025N008 RATON PASS (RWIS)",
"global:Status": "enabled",
"global:EntityId": "1",
"global:RoadId": "31",
"global:RoadName": "I-25",
"global:Direction": "North",
"global:MileMarker": "8",
"global:LastUpdateDate": "2012-08-26T13:52:00.000-06:00",
"global:Icon": "theme\/cotrip.org\/images\/devices\/icon_device_weather_station_with_cam_16x23.gif"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
以下代码为我提供了输出警报,但没有数据:
var weather = $.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=f43ccc14ab9d1c88d537236e93c1525e&_render=json&_callback=?", function(json) {
alert("JSON Data: " + json.value["items"]);
});
Run Code Online (Sandbox Code Playgroud)
我如何进一步遍历这个JSON到'ws:Devise - "global:CommonName"
可能重复:
如何在JavaScript中解析JSON
我得到这样的字符串
[["1","Pagamento de Placa","N","N"], ["2,""Contrato","N","N"], ["3","Ajuste de preço","N","N"],["4", "Abertura de cliente","N","N"], ["9","Outros","N","S"]]
Run Code Online (Sandbox Code Playgroud)
我试试
var arr = new Array([["1","Pagamento de Placa","N","N"], ["2,""Contrato","N","N"], ["3","Ajuste de preço","N","N"],["4", "Abertura de cliente","N","N"], ["9","Outros","N","S"]]);
Run Code Online (Sandbox Code Playgroud)
但不行
我想变成一个真正的数组,任何想法怎么办?
谢谢
我无法从下面的JSON字符串中成功获取数据.使用JavaScript,我能够提醒完整的字符串[alert(data); ]但我无法得到第一个名字.
有人可以帮忙吗?
var data = {
"name": [
"Enid Norgard",
"Cassie Durrett",
"Josephine Ervin"
],
"email": [
"TheWoozyGamer@gmail.com",
"TheHabitualGamer@gmail.com",
"TheUptightGamer@gmail.com"
],
"role": [
"Gamer",
"Team Leader",
"Player"
],
"emp_id": [
"50",
"408",
"520"
],
"id": [
"234",
"444",
"235"
]
}
Run Code Online (Sandbox Code Playgroud) note:
{"category_id":"1","name":"Notes","icon":"images\/note.png"},
quote:
{"category_id":"2","name":"Quotes","icon":"images\/quote.png"},
project:
{"category_id":"3","name":"Projects","icon":"images\/project.png"},
skill:
{"category_id":"4","name":"Skills","icon":"images\/skill.png"}
Run Code Online (Sandbox Code Playgroud)
这是我的控制台中显示的内容.我已经用于json_decode我的查询,并与字符串连接,我相信这是问题的原因.
那么如何将字符串数据转换为json格式?
联系到这个问题,并且这其中也,我想知道如何做同样的事情,但有一个不同类型的JSON的:
这是我的JSON对象:
var myDb = {
"04c85ccab52880": {
"name": "name1",
"firstname": "fname1",
"societe": "soc1"
},
"04c95ccab52880": {
"name": "name2",
"firstname": "fname2",
"societe": "soc2"
},
"048574220e2a80": {
"name": "name3",
"firstname": "fname3",
"societe": "soc3"
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是我的JSON与他们的JSON不同,实际上我有一个数组数组.那么如何将其转换为Javascript数组呢?
var arr = [];
$.each( myDb, function( key, value ) {
arr.push( value );
});
console.log(user_list);
Run Code Online (Sandbox Code Playgroud)
这种脚本似乎返回了我的3个不同的对象但是我的uid来自JSON?我怎么能得到它们?因为现在我的钥匙是0,1,2而不再是我的uid.
有任何想法吗 ?谢谢
我知道如何使用对象,但我想知道...是否可以像这样转换对象键:
{"fname":"peter", "lastname":"parker"}
Run Code Online (Sandbox Code Playgroud)
到这个?
var fname = "peter";
var lname = "parker";
Run Code Online (Sandbox Code Playgroud) 我无法从JSON文件中获取值,该文件已从Steam API(http://steamcommunity.com/dev)中检索到.
当我尝试打印数据时出现问题,Node.js文件代码如下:
var data = JSON.parse(body); // Stores the JSON data which has been retrieved
console.log(data.result.toString(350462890).market_hash_name); // Attempts to grab the value of the market_hash_name from the JSON data and display it to screen
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:"undefined".
下面使用的JSON数据:
{
"result": {
"350462890": {
"icon_url": "fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5-OOqhNQh0fTvSAK5KVPAoywXpDS4n5YliBtazruNQfgrssNfPN-IqYtkdSpTZU_OCYAir70luiaAPfZOIqHznw223bZvDH3kW",
"icon_url_large": "fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZYMUrsm1j-9xgEObwgfEh_nvjlWhNzZCveCDfIBj98xqodQ2CZknz5-OOqhNQh0fTvSAK5KVPAoywXpDS4n5fhvVcWx8vUHe126vYrANLYvNI1FG5LWCPfXM1304048hqALKpffqSu9jyvoMjgCRVO1rexMsCC1",
"icon_drag_url": "",
"name": "Dual Berettas | Panther",
"market_hash_name": "Dual Berettas | Panther (Field-Tested)",
"market_name": "Dual Berettas | Panther (Field-Tested)",
"name_color": "D2D2D2",
"background_color": "",
"type": "Mil-Spec Grade Pistol",
"tradable": "1",
"marketable": "1",
"commodity": …Run Code Online (Sandbox Code Playgroud) 我正在使用C#.Net并试图解析一些JSON.
我有这个我检索的JSON.我试过使用JavaScriptSerliazer,但无法弄清楚正确的结构.
解析此JSON以获取每个数组(第一,第二,第三等)的最佳方法是什么?
{
"first":["A","B"],
"second":["C","D"],
"third":["E","F","G","H","I"],
"fourth":["J","K","L","M","N"]
}
Run Code Online (Sandbox Code Playgroud)