2013-09-05 135 views
0

我是一個新手PHP的傢伙。所以我需要json數據爲我的ios應用程序。我可以從我的數據庫中獲取數據並打印爲json ..我想用json發送許多類型的數據。所以我需要編輯我的JSON輸出。 特此JSON輸出,我現在有:是否可以編輯Json輸出?

[{"PlaceId":"1","PlaceAdminId":"5","PlaceName":"Alinin Yeri","PlaceAddress":"Be?ikta?","PlaceImage1":"iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAAHGlET1QAAAACAAAAAAAAACgAAAAoAAAAKAAAACgAAADWVJb57gAAAKJJREFUeAHs1LEJADAMA8Hsv3SyQ6RGcIVLG\/FYf+69x\/wzAC98IAAB\/K9fQ10+0Af6wOkWTIdvOCy9ASAHcuB0C6bDp\/5q7APIgRw43YLp8A2HpTcA5EAOnG7BdPjUX419ADmQA6dbMB2+4bD0BoAcyIHTLZgOn\/qrsQ8gB3LgdAumwzcclt4AkAM5cLoF0+FTfzX2AeRADpxuwXT4hsPSGw8AAP\/\/ttPzCAAAAKBJREFU7dSxCQAwDAPB7L90skOkRnCFSxvxWH\/uvcf8MwAvfCAAAfyvX0NdPtAH+sDpFkyHbzgsvQEgB3LgdAumw6f+auwDyIEcON2C6fANh6U3AORADpxuwXT41F+NfQA5kAOnWzAdvuGw9AaAHMiB0y2YDp\/6q7EPIAdy4HQLpsM3HJbeAJADOXC6BdPhU3819gHkQA6cbsF0+IbD0hsPozm5V7YwxV0AAAAASUVORK5CYII=","PlaceImage2":null,"PlaceImage3":null,"PlaceImage4":null}, 

{"ActivityId":"1","ActivityName":"zulfu livaneli","ActivityDescription":"Zulfu konseri","ActivityType":"1","ActivityDate":"2011-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}, 

{"ActivityId":"2","ActivityName":"Bir tat bir doku","ActivityDescription":"Y?lmaz Erdo?an oyunu","ActivityType":"3","ActivityDate":"2013-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}, 

這是我多麼希望:

[Places:{{"PlaceId":"1","PlaceAdminId":"5","PlaceName":"Alinin Yeri","PlaceAddress":"Be?ikta?","PlaceImage1":"iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAYAAACOEfKtAAAAHGlET1QAAAACAAAAAAAAACgAAAAoAAAAKAAAACgAAADWVJb57gAAAKJJREFUeAHs1LEJADAMA8Hsv3SyQ6RGcIVLG\/FYf+69x\/wzAC98IAAB\/K9fQ10+0Af6wOkWTIdvOCy9ASAHcuB0C6bDp\/5q7APIgRw43YLp8A2HpTcA5EAOnG7BdPjUX419ADmQA6dbMB2+4bD0BoAcyIHTLZgOn\/qrsQ8gB3LgdAumwzcclt4AkAM5cLoF0+FTfzX2AeRADpxuwXT4hsPSGw8AAP\/\/ttPzCAAAAKBJREFU7dSxCQAwDAPB7L90skOkRnCFSxvxWH\/uvcf8MwAvfCAAAfyvX0NdPtAH+sDpFkyHbzgsvQEgB3LgdAumw6f+auwDyIEcON2C6fANh6U3AORADpxuwXT41F+NfQA5kAOnWzAdvuGw9AaAHMiB0y2YDp\/6q7EPIAdy4HQLpsM3HJbeAJADOXC6BdPhU3819gHkQA6cbsF0+IbD0hsPozm5V7YwxV0AAAAASUVORK5CYII=","PlaceImage2":null,"PlaceImage3":null,"PlaceImage4":null}}, 

Activities:{{"ActivityId":"1","ActivityName":"zulfu livaneli","ActivityDescription":"Zulfu konseri","ActivityType":"1","ActivityDate":"2011-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}, 

{"ActivityId":"2","ActivityName":"Bir tat bir doku","ActivityDescription":"Y?lmaz Erdo?an oyunu","ActivityType":"3","ActivityDate":"2013-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}, 

{"ActivityId":"3","ActivityName":null,"ActivityDescription":"Y?l?n son derbisi","ActivityType":"2","ActivityDate":"2012-00-00","PlaceName":"Alinin Yeri","PlaceId":"1"}}] 

和我的PHP代碼:

$showresult = mysql_query($sqlQuery1); 
$showresult1 = mysql_query($sqlQuery2); 
$multi_array = array(); 
$multi_array1 = array(); 
while($row = mysql_fetch_assoc($showresult)){ 
    $multi_array[] = $row; 
} 
while($row = mysql_fetch_assoc($showresult1)){ 
$multi_array1[] = $row; 
} 
$result = array_merge($multi_array1, $multi_array); 
print json_encode($result); 

編輯 或者是還有什麼方法做我想做的事?

+0

我很確定這是可能的 –

+0

今天我聽到的最好的句子。你知不知道怎麼? – yatanadam

+0

這個過程並不叫做「編輯」,它可能被稱爲「按照我需要的方式構建json」 –

回答

2

將代碼分解爲兩部分。一部分從數據庫獲取「位置」行,第二部分從數據庫獲取「活動」行。然後你做類似

$php_result=array(
"Places"=> $places_rows, 
"Activities"=> $activities_rows 
); 

$json_result= json_encode($php_result); 
+0

那肯定是我想要的。謝謝馬特。我測試,它的工作原理。我認爲在客戶端resultDictionary objectForKey:「地方」會給我地方作爲數組,對嗎? – yatanadam

相關問題