2016-08-12 106 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