【问题标题】:how to convert the string received in php to json array and loop through it?如何将php中接收到的字符串转换为json数组并循环遍历?
【发布时间】:2015-04-03 05:23:21
【问题描述】:

我正在发送一个使用来自 android 的 StringEntity 类转换为 String 的 json 数组。我在 php 中收到如下字符串。

   {
    "introduceesJson": [
        {
            "infoName": "Aa",
            "direction": "2",
            "version": "1",
            "infoNumber": "96 35 874125"
        },
        [
            {
                "infoName": "Aa",
                "direction": "2",
                "version": "1",
                "infoNumber": "96 35 874125"
            }
        ]
    ]
}

现在,我想将其转换为 json 数组并循环遍历它。伙计们,请帮助我。任何帮助,将不胜感激。

【问题讨论】:

  • 使用 json_decode 解码并保存在数组中
  • @habibulhaq,请输入密码。请。

标签: php android arrays json


【解决方案1】:
try {
            JSONArray arr = new JSONArray("Your_String");
            for (int i=0;i<arr.length();i++) {
                JSONObject object = arr.getJSONObject(i);
                String data = object.getString("Your_key");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

【讨论】:

  • 不是java。如何在 php 中循环?无论如何,非常感谢您的回复
【解决方案2】:

试试这个。

 $a='  {
        "introduceesJson": [
            {
                "infoName": "Aa",
                "direction": "2",
                "version": "1",
                "infoNumber": "96 35 874125"
            },
            [
                {
                    "infoName": "Aa",
                    "direction": "2",
                    "version": "1",
                    "infoNumber": "96 35 874125"
                }
            ]
        ]
    }';

    $res=json_decode($a,true);
    $data=array();
    $data=$res['introduceesJson'];
    echo sizeof($data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 2018-10-19
    • 2016-11-05
    • 2019-02-27
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多