PHP json解析。Don';t集重复

PHP json解析。Don';t集重复,php,arrays,json,parsing,Php,Arrays,Json,Parsing,请确保其不重复请使我的json以以下内容开头: [ { } ] 没有根元素。我不知道如何把这个解析为你的答案。我是php新手。目前正在开发一个android应用程序,向php发送json字符串。。我的json数据是字符串格式的。 如何在php中解析此字符串。我需要这个字符串中的每个id和name存储在数据库中 [ {"name":"Abhishek Singh", "photo":"http:\........./diary\/photos\/student\/26.png",

请确保其不重复请使我的
json
以以下内容开头:

[
 {

 }
]
没有根元素。我不知道如何把这个解析为你的答案。我是php新手。目前正在开发一个android应用程序,向php发送json字符串。。我的json数据是字符串格式的。 如何在php中解析此字符串。我需要这个字符串中的每个
id
name
存储在数据库中

[
 {"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"
 }
]
试试这个:

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

首先,您的照片值不正确,它的值应该是

"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;
}
解析

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();
   }
试试看{
JSONArray jr=新JSONArray(“字符串名称”);
JSONObject jb=(JSONObject)jr.getJSONObject(0);
JSONArray st=jb.getJSONArray(“key”);

对于(int i=0;i迭代多维数组,可以使用递归数组迭代器

$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

试着使用json_decode你可以在@AbhishekSingh的可能副本上看到它的详细信息:用PHP标记发布它会得到更好的响应可能的副本我认为这是一个有效的问题。我记得几年前我开始使用json,我只有一年多一点的PHP经验,使用json会让人非常困惑。。。我说把问题留在这里……好吧,你们有正确的观点……我必须用php而不是android解析……我将json发布到php中。@Deo仔细阅读问题。我从android@AbhishekSingh仍然不需要添加标签
android
这里的每个人都认为你想在android中解析json…注意e下次..很快你的问题就要结束了。@AbhishekSingh检查我的..如果有什么帮助的话,试着向上投票..这就是@MaňishYadav的好答案,我想:-)它的形式是正确的,我在那里加了点,因为我没有分享链接,所以我希望这个答案能解决你的问题。祝你好运。@AbhishekSingh记住这些事情……并且在这方面有很好的声誉
John:
status => Not Active
Jennifer:
status => Active
James:
status => Active
age => 52
count => 14
progress => 29857
bad => 10