-1

Here is my php file

<?php
    $myObj = array(
        "name"=>"John" ,
        "age"=>"30" ,
        "post"=>[
            "title"=>"what is WordPress" ,
            "excerpt"=>"WordPress is a famous blogging cms" ,
            "description"=>"WordPress is a famous which is used by million bloggers",
            "meta"=>[
                "timing"=>["time"=>"3pm", "date"=>"3Jan"]
             ]
         ]
    );
    $obj = json_encode($myObj);
    echo $obj;
?>

And it returns like this.

{"name":"John","age":"30","post":{"title":"What is WordPress","excerpt":"WordPress is an blogging cms","description":"WordPress is an blogging cms used by million of bloggers","meta":{"time":"3pm","date":"3Jan"}}}

I used HttpUrlConnection and this catch the JSON. When I request for the title, excerpt, the description I get the expected result.

But I want to get the result of "time" of meta->timing section.

Please help me how to get it. I don't know if it is possible or not.

I think adding the Java code is not so much important So hadn't added it.

4
  • You should add a sample of the code that you wrote to decode the JSON on Android to actually tell you what's wrong. Commented Jan 11, 2018 at 15:03
  • @ValerioSantinelli JSONObject job = new JSONObject(result); JSONObject pjob = job.getJSONObject("post") .getJSONObject("meta") .getJSONObject("timing"); /*JSONArray jarr = new JSONArray("post");*/ Toast.makeText(getApplicationContext(), pjob.getString("time"), Toast.LENGTH_LONG); But it returns exception like type of String cannot be coverted to JSONObject Commented Jan 11, 2018 at 15:06
  • This is wrong. You're reading the timing property of the meta object but there's no such property. You should instead read the time property as suggested by vikas kumar. Commented Jan 11, 2018 at 15:09
  • @ValerioSantinelli Thanks, but notice what my php code returns. After noticing that myself I have changed the code and said to you. Commented Jan 11, 2018 at 15:16

3 Answers 3

1

it's easy I hope you are using the default JSONObject of android API then just pass the raw json string and it will give you back the time.

so create something like this.

public String getTime(String json){
    JSONObject obj = new JSONObject(jsonString);
    JSONObject post=  obj.getJSONObject("post");
    JSONObject meta = post.getJSONObject("meta");
    String time = meta.getString("time");
    return time;
}
Sign up to request clarification or add additional context in comments.

4 Comments

JSONObject job = new JSONObject(result); JSONObject pjob = job.getJSONObject("post") .getJSONObject("meta"); Toast.makeText(getApplicationContext(), pjob.getString("time"), Toast.LENGTH_LONG); I jave used this code but toasts type of string cannot be coverted to JSONObject
can you post your code using which you are making toast and the json you posted is same as in question?
I have tried what you have suggested recently. I want to toast the time from the returning json I have added above.
your android code bro ? how are you getting the json and making toast please share. because your json simple and there should not be any problem trying above answer if you can share java code how are you doing then we can help you.
0

Considering your PHP code, you should probably do something like this:

public String getTime(String json){
    JSONObject obj = new JSONObject(json);
    JSONObject post=  obj.getJSONObject("post");
    JSONObject meta = post.getJSONObject("meta");
    JSONObject timing = post.getJSONObject("timing");
    String time = timing.getString("time");
    return time;
}

4 Comments

I have tried this but same result. It says Type of String cannot be coverted to JSONObject
You should definitely edit your answer and paste the actual output of the PHP code you've written so that we can help you out
I have turned my localhost on and copied the result. {"name":"John","age":"30","post":{"title":"What is WordPress","excerpt":"WordPress is an blogging cms","description":"WordPress is an blogging cms used by million of bloggers","meta":{"time":"3pm","date":"3Jan"}}}
The code you pasted in the comment up there isn't coming from the PHP code you posted in your question. The properties have different names. Either your code is wrong or your output is wrong
0

Function to retrieve time object

public String getTime(String json){
    try {
        JSONObject obj = new JSONObject(json);
        JSONObject post = obj.getJSONObject("post");
        JSONObject meta = post.getJSONObject("meta");
        return meta.getString("time");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return "error retrieve Time";
}

4 Comments

Are everyone copying each other? They also said this answer and I have answered what?
What's your problem now? I didn't copy anyone.
To show a Toas in your activity : Toast.makeText(this, getTime(json), Toast.LENGTH_LONG).show(); Out of activity pass a reference to your activity and substitute to "this".
this returns Activity not any Context. Okay?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.