使用javascript将JSON数组转换为单个变量

时间:2016-08-12 06:10:27

标签: javascript php json

我有一个从API输出的以下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

将此数组拆分为单独的变量

4 个答案:

答案 0 :(得分: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 :(得分:1)

Json Code

{
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;
}
?>

希望这会有所帮助。 :)

答案 2 :(得分:1)

这就是你如何在不同的JS变量中获得国家和它的临时性。

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

其中{ }是Object的文字符号,使用{ }运算符访问.内的任何属性,而[ ]是一个文字表示法阵列。你可以使用索引(例如[0],[1]等...)来访问数组元素,就像普通的JS数组一样......)

答案 3 :(得分: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>

JSON.parse(str)用于将字符串转换为对象

enter image description here