vikrant verma
vikrant verma

Reputation: 159

How to use the Multiple Json Objects encoded by php in Android

I have php file in which iam using the different variables and encode them to objects like:-

$abc=array();

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

$bcd=array();

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

echo json_encode($abc);

echo  json encode($bcd);

Now in android

when i use the response listner how can i parse these two objects seprately iam able to create single json object easily like:-

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();
        }
    }
};

Upvotes: 3

Views: 488

Answers (1)

ABelikov
ABelikov

Reputation: 612

Just echo them as one array PHP

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

So in your java code you can get them by key

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

Or always work with ob by get()

Upvotes: 1

Related Questions