Rumah > pembangunan bahagian belakang > tutorial php > 如何从Json中取出数据放到一个新组中

如何从Json中取出数据放到一个新组中

WBOY
Lepaskan: 2016-06-23 13:55:35
asal
1053 orang telah melayarinya

JSON数据如下:

{    "CommunityModel": [        {            " UUID ": "xxxxxx-xxxxxx-xxxxxxx-xxxxxx1",            " CommunityName ": "格林花园",            " CommunityAddress ": "XXXX203号",            " Longitude ": "12.33333333",            " Latitude ": "143.1121222",            " Form ": "商品房",            " BuildingNum ": "100",            " OwnerNum ": "1800",            " CarportNum ": "1800"        },{            " UUID ": "xxxxxx-xxxxxx-xxxxxxx-xxxxxx2",            " CommunityName ": "格林花园2",            " CommunityAddress ": "XX路203号",            " Longitude ": "12.33333333",            " Latitude ": "143.1121222",            " Form ": "商品房",            " BuildingNum ": "100",            " OwnerNum ": "1800",            " CarportNum ": "1800"        }    ]}
Salin selepas log masuk

通过PHP获取,要求获取JSON中UUID和CommunityName放到一个新数组$arr中返回。

我的代码如下:
		                $url_get ='http://api.com:90/1.php';		$json=json_decode($this->curlGet($url_get));		foreach($json as $jsonvalue)		{                      。。。。这里该如何写	//		dump($jsonvalue);		}
Salin selepas log masuk


回复讨论(解决方案)

好奇怪,居然键名两端是空格

$s =<<< JSON{    "CommunityModel": [        {            " UUID ": "xxxxxx-xxxxxx-xxxxxxx-xxxxxx1",            " CommunityName ": "格林花园",            " CommunityAddress ": "XXXX203号",            " Longitude ": "12.33333333",            " Latitude ": "143.1121222",            " Form ": "商品房",            " BuildingNum ": "100",            " OwnerNum ": "1800",            " CarportNum ": "1800"        },{            " UUID ": "xxxxxx-xxxxxx-xxxxxxx-xxxxxx2",            " CommunityName ": "格林花园2",            " CommunityAddress ": "XX路203号",            " Longitude ": "12.33333333",            " Latitude ": "143.1121222",            " Form ": "商品房",            " BuildingNum ": "100",            " OwnerNum ": "1800",            " CarportNum ": "1800"        }    ]}JSON;$a = json_decode($s, 1);foreach($a['CommunityModel'] as $v) {  $arr[] = array('UUID' => $v[' UUID '], 'CommunityName' => $v[' CommunityName ']);}print_r($arr);
Salin selepas log masuk
Array(    [0] => Array        (            [UUID] => xxxxxx-xxxxxx-xxxxxxx-xxxxxx1            [CommunityName] => 格林花园        )    [1] => Array        (            [UUID] => xxxxxx-xxxxxx-xxxxxxx-xxxxxx2            [CommunityName] => 格林花园2        ))
Salin selepas log masuk

 $val[' UUID '],        'CommunityName' => $val[' CommunityName ']    );}print_r($arr);?>
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan