在php中解析JSON文件 [英] Parsing JSON file in php

查看:62
本文介绍了在php中解析JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想解析一个JSON文件,其中包含如下所示格式的数据,



[

{

国家:美国

国家:[

{

城市:[

Abbeville,

Abernant,

Adamsville ,

Addison

],

州:AL

},

{

城市:[

Adak,

Akiak,

Akutan,

Ambler

],

州:AK

},

]

},

{

国家:加拿大

州:[

{

城市:[

Barrie,

贝尔维尔,

布兰普顿,

布兰特

],

州:开

},

{

城市:[

Acton Vale,

Alma,

Amos,
Amqui,

石棉

],

州:QC

},

]

}



]



从JSON格式上面,我想得到所有国家名称,州名,城市名称的列表。



首先,我想要所有的国家名单。然后当我将国家名称作为加拿大传递时,应该获取其所有州,依此类推以获取相应国家,州和城市的城市名称。

Hi All,

I want to parse a JSON files which contains data in the format as shown below,

[
{
"Country": "United States"
"States": [
{
"Cities": [
"Abbeville",
"Abernant",
"Adamsville",
"Addison"
],
"State": "AL"
},
{
"Cities": [
"Adak",
"Akiak",
"Akutan",
"Ambler"
],
"State": "AK"
},
]
},
{
"Country": "Canada"
"States": [
{
"Cities": [
"Barrie",
"Belleville",
"Brampton",
"Brant"
],
"State": "ON"
},
{
"Cities": [
"Acton Vale",
"Alma",
"Amos",
"Amqui",
"Asbestos"
],
"State": "QC"
},
]
}

]

From above JSON format, I want to get list of all Country names, State names,City Names.

First,I want all the country names list. Then when I pass country name as "Canada" then all its states should be fetched and so on to get the city names of respective country ,state and city.

推荐答案

请参阅:

http://php.net/manual/en/book.json .php [ ^ ],

http://php.net/manual/en/function.json-decode .php [ ^ ] 。



-SA
Please see:
http://php.net/manual/en/book.json.php[^],
http://php.net/manual/en/function.json-decode.php[^].

—SA


首先,将你的json数据读成一个字符串。

First, read your json data into a string.


strJson = file_get_contents(' data.json');
strJson = file_get_contents('data.json');





第二,解码jso n数据到数组变量



Second, decode the json data into an array variable


这篇关于在php中解析JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆