2016-08-12 108 views
0

我有以下从API输出的JSON数组。使用javascript将JSON数组转换为单个变量

{ 
    "location": { 
     "name": "Alanallur", 
     "region": "Kerala", 
     "country": "India", 
     "lat": 11.01, 
     "lon": 76.33, 
     "tz_id": "Asia/Kolkata", 
     "localtime_epoch": 1470998311, 
     "localtime": "2016-08-12 10:38" 
    }, 
    "current": { 
     "last_updated_epoch": 1470997826, 
     "last_updated": "2016-08-12 10:30", 
     "temp_c": 28.0, 
     "temp_f": 82.4, 
     "is_day": 1, 
     "condition": { 
      "text": "Moderate rain", 
      "icon": "//cdn.apixu.com/weather/64x64/day/302.png", 
      "code": 1189 
     }, 
     "wind_mph": 8.1, 
     "wind_kph": 13.0, 
     "wind_degree": 340, 
     "wind_dir": "NNW", 
     "pressure_mb": 1013.0, 
     "pressure_in": 30.4, 
     "precip_mm": 0.0, 
     "precip_in": 0.0, 
     "humidity": 79, 
     "cloud": 0, 
     "feelslike_c": 32.2, 
     "feelslike_f": 89.9 
    } 
} 

我想使用JavaScript

+1

1)你没有阵列存在,2)JSON没有阵列,它是一个字符串,3)解析时,你将有一个JavaScript OB ject,没有数组 –

+0

您在JSON字符串中没有数组。 http://json-parser.com/6d51ab22你想要哪个字段作为单独的变量? –

+0

'location'和'current'是两个结果变量吗? – guest271314

回答

1

如果这种反应是data

check alert(typeof data) // it will show object or string 

如果现在不那么物体

data= JSON.parse(data) 

您拆分此阵成独立的变量可以访问它作为变量

,如果你想访问本地时间位置,然后

alert(data.location.localtime) 

我希望这意义上的jsfiddle

你 检查两个例子

https://jsfiddle.net/5ov827oc/1/

https://jsfiddle.net/5ov827oc/2/

1

的Json代码

{ 
posts:[ 
    "location": { 
     "name": "Alanallur", 
     "region": "Kerala", 
     "country": "India", 
     "lat": 11.01, 
     "lon": 76.33, 
     "tz_id": "Asia/Kolkata", 
     "localtime_epoch": 1470998311, 
     "localtime": "2016-08-12 10:38" 
    }, 
    "current": { 
     "last_updated_epoch": 1470997826, 
     "last_updated": "2016-08-12 10:30", 
     "temp_c": 28.0, 
     "temp_f": 82.4, 
     "is_day": 1, 
     "condition": { 
      "text": "Moderate rain", 
      "icon": "//cdn.apixu.com/weather/64x64/day/302.png", 
      "code": 1189 
     }, 
     "wind_mph": 8.1, 
     "wind_kph": 13.0, 
     "wind_degree": 340, 
     "wind_dir": "NNW", 
     "pressure_mb": 1013.0, 
     "pressure_in": 30.4, 
     "precip_mm": 0.0, 
     "precip_in": 0.0, 
     "humidity": 79, 
     "cloud": 0, 
     "feelslike_c": 32.2, 
     "feelslike_f": 89.9 
    } 
} 

在JavaScript中使用

json.parse() 

PHP代码

<?php 
// copy file content into a string var 
$json_file = file_get_contents('posts.json'); 
// convert the string to a json object 
$jfo = json_decode($json_file); 
// read the title value 
$title = $jfo->result->title; 
// copy the posts array to a php var 
$posts = $jfo->result->posts; 
// listing posts 
foreach ($posts as $post) { 
    echo $post->title; 
} 
?> 

希望这有助于。 :)

1

这是如何得到国家,它是在单独的JS变量temparutre。

var jsonobject=JSON.parse(json_string); // don't do this if you already have JSON Object instead of string. 
var country=jsonobject.location.country. // Where location is another JSON Object 
var temprature_c=jsonobject.current.temp_c; // current is another JSON Object 
var temprature_f=jsonobject.current.temp_f; // current is another JSON Object 

看到这个来调试你的JSON。 http://json-parser.com/6d51ab22

哪里{ }是一个对象的一个​​文字符号,里面访问任何{ }与物业运营.
[ ]是数组中的文字符号。可以访问数组元素就像使用索引正常JS阵列(例如[0],[1]等)

+0

“你继续使用这个词,我不认为这意味着你认为它的意思。” [没有像“JSON对象”](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/)。 –

+0

{}简单地表示它是具有键值对的对象。 –

+0

我原来的评论是一个笑话,因为很多人犯了这个错误,因此[公主新娘](https://www.youtube.com/watch?v=wujVMIYzYXg)引用。然而,要澄清一点:JSON是一个字符串类型,而不是一个对象类型。 JSON是一个首字母缩写词,意思是** J ** ava ** S ** cript ** O ** bject ** N ** otation。在你的回答中,你声明“'{'''表示这是一个明显错误的JSON对象”。它应该像“'{}'是对象的文字符号”那样读取,如果你想要准确的话。无论如何,我希望这有助于理解差异。 –

1

尝试此代码: 使用显影剂控制台来查看在浏览器中的输出

<script> 
var str = '{"location":{"name":"Alanallur","region":"Kerala","country":"India","lat":11.01,"lon":76.33,"tz_id":"Asia/Kolkata","localtime_epoch":1470998311,"localtime":"2016-08-12 10:38"},"current":{"last_updated_epoch":1470997826,"last_updated":"2016-08-12 10:30","temp_c":28,"temp_f":82.4,"is_day":1,"condition":{"text":"Moderate rain","icon":"//cdn.apixu.com/weather/64x64/day/302.png","code":1189},"wind_mph":8.1,"wind_kph":13,"wind_degree":340,"wind_dir":"NNW","pressure_mb":1013,"pressure_in":30.4,"precip_mm":0,"precip_in":0,"humidity":79,"cloud":0,"feelslike_c":32.2,"feelslike_f":89.9}}'; 

if(typeof(str) == 'string'){ 
     str = JSON.parse(str); 
     console.log(str.current)//return object 
     console.log(str.location)//return object 
     console.log(str.current.temp_c)//return string 
     console.log(str.current.temp_f)//return string 
     console.log(str.location.country)//return string 
} 
</script> 

​​用来将字符串转换为对象

enter image description here