【问题标题】:How to use the Multiple Json Objects encoded by php in Android如何在Android中使用php编码的多个Json对象
【发布时间】:2018-08-18 17:38:06
【问题描述】:

我有一个 php 文件,其中 iam 使用不同的变量并将它们编码为对象,例如:-

$abc=array();

$abc['name']="Verma";

$bcd=array();

$bcd=['xyz']="Sharma";

echo json_encode($abc);

echo  json encode($bcd);

现在在安卓中

当我使用响应侦听器时,如何分别解析这两个对象,我能够轻松地创建单个 json 对象,例如:-

final Response.Listener<String> resd= new Response.Listener<String>(){
    @Override
    public void onResponse(String response) {
        try {
            JSONObject ob= new JSONObject(response); // here how can i reffer to other json ojbect

            String data=ob.getString("viki");
            txt.setText(data);
        } catch (JSONException e) {
            AlertDialog.Builder j=new AlertDialog.Builder(MainActivity.this);
            j.setCancelable(true);
            j.setMessage(e.getMessage());
            j.show();
        }
    }
};

【问题讨论】:

  • 嗯,你可以从服务器返回json数组并在这里解析
  • 这就是我要问的如何在android中解析jsonobjectarray

标签: php android json android-studio


【解决方案1】:

只需将它们作为一个数组 PHP 回显

echo json_encode(
'abc'=>$abc,
'bcd'=>$bcd
);

所以在你的java代码中你可以通过key获取它们

JSONObject ob = new JSONObject(response);
JSONObject abc = ob.get("abc");
JSONObject bcd = ob.get("bcd");

或者总是通过 get()

使用 ob

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 2014-11-24
    • 2013-09-05
    相关资源
    最近更新 更多