0

i've got a problem to retrieve information by a JSON

The Raw Json is this

{"date":"{\"yesterday\":\"Wed 28\",\"today\":\"Thu 29\",\"tomorrow\":\"Fri 30\"}"

Now how i could take this Json and format in this?

{
    "date":{
        "yesterday":"Wed 28",
        "today":"Thu 29",
        "tomorrow":"Fri 30"
    }
}

And then retry the date from the key?

            String jsonStr = sh.makeServiceCall(url); 
            Log.e("RAW-JSON: ","Retrieve RAW-Json is "+jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    JSONArray DATESTRING = jsonObj.getJSONArray("date");
                    JSONObject d = DATESTRING.getJSONObject(0);
                    String Ieri = d.getString("yesterday");
                    Log.e("DATE-JSON", "Retrieve DATE-Json is " + yesterday);
                } catch (JSONException e) {
                    Log.e("ERROR", "Not a good result.");
                    e.printStackTrace();
                }
            }
0

4 Answers 4

2

I would strongly recommend use the library Gson to parse a JSON document, is much easier and elegant.

public static date  parseJSON(String jsonArray) {
    date yourDate = new date();
    try {
        yourDate = (gson.fromJson(jsonArray, date.class));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return yourDate;
}

Then the class date is just a class with the same elements that exists in the JSON you want to read in your case:

public class date {
    private String yesterday;
    private String today;
    private String tomorrow;

}
Sign up to request clarification or add additional context in comments.

Comments

0

JSONArray DATESTRING = jsonObj.getJSONArray("date") is wrong because "date" is not an JsonArray but an JsonObject!

4 Comments

Ok how can i retrieve data? Exists some java class where if i give the key they return the argument of Json?
jsonObj.getJSONObject("date").getString("today");
Value {"yesterday":"Wed 28","today":"Thu 29","tomorrow":"Fri 30"} at date of type java.lang.String cannot be converted to JSONObject
Unterminated object at character 12 of all of the rest of my json
0

What you probably need to do is (assuming you want today's date):

String todaysDate = jsonObj.getJSONObject("date").getString("today");

2 Comments

i've got this exc org.json.JSONException: Value {"yesterday":"Wed 28","today":"Thu 29","tomorrow":"Fri 30"} at date of type java.lang.String cannot be converted to JSONObject
what if you get rid of the escape chars and create a new object? like there has been stated do this : jsonStr = jsonStr.replace("\\\"", "\""); JSONObject jsonObj = new JSONObject(jsonStr); and then what I told you will probably work.
0

Just simply replace \" with " to get clean json string and then parse the json to read data usig JSONObjct.

jsonStr = jsonStr.replace("\\\"", "\"");
JSONObject jsonObj = new JSONObject(jsonStr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.