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

使用javascript将JSON数组转换为单个变量,javascript,php,json,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, "lo

我有以下从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将此数组拆分为单独的变量

check alert(typeof data) // it will show object or string
如果它不是对象,那么

data= JSON.parse(data)
现在您可以作为saperate变量访问它

如果要访问位置的localtime,则

alert(data.location.localtime)
我希望这对你有意义 检查JSFIDLE上的两个示例


如果此响应在
数据中

check alert(typeof data) // it will show object or string
如果它不是对象,那么

data= JSON.parse(data)
现在您可以作为saperate变量访问它

如果要访问位置的localtime,则

alert(data.location.localtime)
我希望这对你有意义 检查JSFIDLE上的两个示例

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

希望这有帮助。:)

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


希望这有帮助。:)

这就是如何在单独的JS变量中获取country和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


如果
{}
是对象的文字符号,请使用
运算符访问
{}
中的任何属性,
[]
是数组的文字符号。您可以使用索引(例如[0]、[1]等)访问数组元素,就像访问普通JS数组一样。

这就是如何在单独的JS变量中获取country和它的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

如果
{}
是对象的文字符号,请使用
运算符访问
{}
中的任何属性,
[]
是数组的文字符号。您可以使用索引(例如[0]、[1]等)访问数组元素,就像访问普通JS数组一样。

请尝试以下代码: 使用开发人员控制台在浏览器中查看输出

<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>

var str='{“地点”:{“名称”:“阿拉纳尔”,“地区”:“喀拉拉”,“国家”:“印度”,“拉特”:11.01,“朗”:76.33,“tz id”:“亚洲/加尔各答”,“当地时间”:1470998311,“当地时间”:“2016-08-12 10:38”},“当前”:{“上次更新的时代”:1470997826,“最后更新的时代”:“2016-08-12 10:30”,“临时c”:28,“临时f”:82.4,“是一天”:1,“条件”:“文本”:“中雨”,“图标”:“//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,“湿度”:79,“云”:0,“feellike_-c”:32.2,“feellike_-f”:89.9}”;
if(typeof(str)=‘string’){
str=JSON.parse(str);
console.log(str.current)//返回对象
console.log(str.location)//返回对象
console.log(str.current.temp_c)//返回字符串
console.log(str.current.temp\u f)//返回字符串
console.log(str.location.country)//返回字符串
}
JSON.parse(str)
用于将字符串转换为对象

请尝试以下代码: 使用开发人员控制台在浏览器中查看输出

<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>

var str='{“地点”:{“名称”:“阿拉纳尔”,“地区”:“喀拉拉”,“国家”:“印度”,“拉特”:11.01,“朗”:76.33,“TZID”:“亚洲/加尔各答”,“当地时间”:1470998311,“当地时间”:“2016-08-12 10:38”},“当前”:{“上次更新的时代”:1470997826,“最后更新的时代”:“2016-08-12 10:30”,“临时c”:28,“临时f”:82.4,“是一天”:1,“条件”:“文本”:“中雨”,“图标”://cdn.apixu.com/weather/64x64/day/302.png,“代码”:1189},“风速”:8.1,“风速”:13,“风速”:340,“风向”:“北纬”、“气压”:1013,“气压”:30.4,“精密度”:0,“精密度”:0,“湿度”:79,“云”:0,“感觉像c”:32.2,“感觉像f”:89.9};
if(typeof(str)=‘string’){
str=JSON.parse(str);
console.log(str.current)//返回对象
console.log(str.location)//返回对象
console.log(str.current.temp_c)//返回字符串
console.log(str.current.temp\u f)//返回字符串
console.log(str.location.country)//返回字符串
}
JSON.parse(str)
用于将字符串转换为对象


1)你没有数组,2)JSON没有数组,它是一个字符串,3)解析时,你会有一个javascript对象,没有数组你在JSON字符串中没有数组。你想要哪个字段作为单独的变量?应该
location
current
作为两个结果变量吗?你需要使用
JSON.parse()
,很抱歉,我是javascript中的一个新b…1)你没有数组,2)JSON没有数组,它是一个字符串,3)解析时,你将有一个javascript对象,没有数组你在JSON字符串中没有数组。你想要哪个字段作为单独的变量?应该
location
current
是两个结果变量b吗les?你需要使用
JSON.parse()
,很抱歉…我是javascript中的新b…”你继续使用这个词。我不认为这意味着你认为这意味着什么简单地说,它是一个带有键值对的对象。我最初的评论是开玩笑的,因为很多人都犯了这个错误,因此引用了这句话。不过,要进一步澄清一下:JSON是一种字符串类型,而不是对象类型。JSON是一个首字母缩写,意思是JavaScript对象表示法。在你的回答中,你要说明“
{}”
表示它是一个JSON对象,“这显然是错误的。如果你想准确,它应该读作“
{}
是对象的文字符号”。无论如何,我希望这有助于理解区别。@JasonCust感谢你的提醒,更正了我的答案:)”你继续使用这个词。我不认为这意味着你认为这意味着什么简单地说,它是一个带有键值对的对象。我最初的评论是开玩笑的,因为很多人都犯了这个错误,因此引用了这句话。不过,要进一步澄清一下:JSON是一种字符串类型,而不是对象类型。JSON是一个首字母缩写,意思是JavaScript对象表示法。在你的回答中,你要说明“
{}”
表示它是一个JSON对象”,这显然是错误的。如果你想准确,它应该读作“
{}
是对象的文字符号”