【问题标题】:PHP json parsing. Don't set DuplicatePHP json 解析。不要设置重复
【发布时间】:2017-01-10 05:09:59
【问题描述】:

伙计们,它的不重复请以我的json开头:

[
 {

 }
]

没有根元素。我不知道如何将其解析为您的答案。我是 php 新手。目前正在开发一个将 json 字符串发送到 php 的 android 应用程序。我的 json 数据是字符串格式的。 如何在 php.ini 中解析这个字符串。我需要将此字符串中的每个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"
 }
]

【问题讨论】:

标签: php arrays json parsing


【解决方案1】:

首先你的照片价值不正确,它的价值应该是这样的

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

【讨论】:

  • 它的格式正确我在那里添加了点导致不共享链接
  • 那么我希望这个答案能解决你的问题。祝你好运。
【解决方案2】:

试试这个:

$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

【讨论】:

    【解决方案3】:

    解析

    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对象节点

    【讨论】:

    • 我必须在 php 中解析而不是在 android 中...我将那个 json 发布到 php..
    • @Deo 仔细阅读了这个问题。我从 android 发送这个 json
    • @AbhishekSingh 仍然不需要添加标签 android 这里的每个人都在想你想在 android 中解析 json ... 下次要小心.. 很快你的问题就会被关闭 .
    • @AbhishekSingh 检查我的answer below.. 如果有任何帮助,请尝试upvote .. 这就是stackoverflow works
    • @MaňishYadav 我认为不错的答案:-)
    【解决方案4】:

    要遍历多维数组,可以使用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
    

    【讨论】:

    • @AbhishekSingh 牢记这些事情......并在 SO 上享有盛誉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    相关资源
    最近更新 更多