2017-01-10 27 views
-3

傢伙的不重複請引起我json開始:PHP json解析。不要設置重複

[ 
{ 

} 
] 

沒有根元素。我不知道如何解析這個作爲你的答案。我是新來的PHP。目前正在開發一個android應用程序,發送一個json字符串到php ..我的json數據是字符串格式。 我如何解析這個字符串在PHP中。我需要每idname從這個字符串中的數據庫來存儲...

[ 
{"name":"Abhishek Singh", 
    "photo":"http:\........./diary\/photos\/student\/26.png", 
    "rollno":"1", 
    "id":"26"}, 
{"name":"Ashutosh Tripathi", 
    "photo":"http:\........./diary\/photos\/student\/34.png", 
    "rollno":"2", 
    "id":"34"}, 
{"name":"Ayushi Srivastava", 
    "photo":"http:\............./diary\/photos\/student\/42.png", 
    "rollno":"3", 
    "id":"42" 
} 
] 
+0

嘗試使用json_decode你可以看到它的de尾巴在http://php.net/manual/en/function.json-decode.php – Abbas

+6

可能重複的[如何從JSON提取數據與PHP?](http://stackoverflow.com/questions/29308898/如何從json提取數據) –

+0

兄弟我是android開發人員,所以我不知道如何獲得$ var中的數據..只是一點點的幫助需要如何從那裏獲得id –

回答

2

您所有的照片價值首先是不正確的它的值應該如

"photo":"http://........./diary\/photos\/student\/26.png" 

"photo":"http:/diary\/photos\/student\/26.png" 

或者一些別的東西不喜歡http:\....

而如果它的值是固定的,那麼它的解決方案是

$json = '[{"name":"Abhishek Singh", 
    "photo":"http://........./diary\/photos\/student\/26.png", 
    "rollno":"1", 
    "id":"26"}, 
{"name":"Ashutosh Tripathi", 
    "photo":"http://........./diary\/photos\/student\/34.png", 
    "rollno":"2", 
    "id":"34"}, 
{"name":"Ayushi Srivastava", 
    "photo":"http://............./diary\/photos\/student\/42.png", 
    "rollno":"3", 
    "id":"42"}]'; 

$obj = json_decode($json); 
foreach ($obj as $key=>$record){ 
    echo $record->id; 
} 
+0

其正確的形式我加了點,因爲沒有共享鏈接 –

+0

然後,我希望這個答案將解決您的問題。祝你好運。 – Abbas

+0

感謝它的工作 –

1

試試這個:

$data = json_decode($json,true);//$json is your json data 

foreach($data as $key=>$value){ 
print_r($value);// this should print each array of json 
} 

DEMO

+0

感謝您的建議,我會嘗試驗證它,如果它的工作..... –

1

爲了解析

try { 

    JSONArray jr = new JSONArray("string_name"); 
    JSONObject jb = (JSONObject)jr.getJSONObject(0); 
    JSONArray st = jb.getJSONArray("key"); 
    for(int i=0;i<st.length();i++) 
     { 
     String id= st.getString(i); 
     String name= st.getString(i+1); 
     Log.i("..........",""+key); 
     // loop and add it to array or arraylist 
     } 
    }catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

一旦你解析,並將其添加數組。使用相同的來填充你的微調器。

[表示JSON數組節點

{表示JSON對象節點

+0

我必須解析在PHP中不是在Android ...我發佈該JSON到PHP .. –

+2

@AbhishekSingh你根本不清楚...爲什麼你使用標籤:android然後??? ? – Gattsu

+0

@Deo仔細閱讀問題。我從android –

1

遍歷多維數組,你可以使用RecursiveArrayIterator

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)), 
    RecursiveIteratorIterator::SELF_FIRST); 

foreach ($jsonIterator as $key => $val) { 
    if(is_array($val)) { 
     echo "$key:\n"; 
    } else { 
     echo "$key => $val\n"; 
    } 
} 

輸出:

John: 
status => Not Active 
Jennifer: 
status => Active 
James: 
status => Active 
age => 52 
count => 14 
progress => 29857 
bad => 10 
+0

@AbhishekSingh記住這些事情......並在SO上有良好的聲譽 – Gattsu