2

I have a json list like this from PHP:

$Json = '[{"test":"1", "message":"try it"}, 
{"test":"2", "message":"try it"}, {"test":"3", "message":"try it"} ...]';
$final = [ 'error' => '1', 'json' => json_encode($Json)];
die(json_encode($final));

From Android i can show the result like this:

JsonParser jsonParser = new JsonParser();
JsonObject res = (JsonObject) jsonParser.parse(new Gson().toJson(response.body()));
Log.w("Return", response.body().toString());

All works fine until now, but when i try to make a new Json Object from the returned results, i get this error message:

com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject

Here what i did:

JsonObject json = (JsonObject) jsonParser.parse(new Gson().toJson(res.get("json").toString()));
Log.w("JSON", json.toString());

Any fix please ?

Jis Maxi
  • 226
  • 1
  • 4
  • 16

1 Answers1

3

First, fix your PHP.

$Json = array(
    array('test' => '1', 'message' => 'try it'), 
    array('test' => '2', 'message' => 'try it')
  );
$final = array(
  'error' => '1', 
  'json' => $Json
);
die(json_encode($final, JSON_FORCE_OBJECT));

You don't need to encode an already valid JSON string.


All works fine

You never used res there. You printed the response body as-is.

Log.w("Return", response.body().toString());

This looks wrong. The response is already a JSON string, so toJson wouldn't be necessary.

jsonParser.parse(new Gson().toJson(response.body()));

Your error is the "JsonPrimitive" in this case is a String, which is not a JSON object.


You should rather do this

final String body = response.body().toString(); // Or use response.raw(), if want raw response string
JsonParser jsonParser = new JsonParser();
JsonObject res = jsonParser.parse(body).getAsJsonObject();
Log.w("Return", res.toString());

If you want the data, then you can have

JsonArray data = res.getAsJsonArray("data");

Alternatively, you will need to make a POJO and deserialize your data list.

class Data {
    String test, message;
}
Graham
  • 7,431
  • 18
  • 59
  • 84
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thank you for the answer, when using your code i am getting this new error: `Not a JSON Object: "com.myapp.model.User@5a1a6fc"` – Jis Maxi Apr 26 '17 at 21:15
  • Well, a User isn't a JSONObject. My answer doesn't use a User class at all, so that is unrelated – OneCricketeer Apr 26 '17 at 21:25
  • My goal is to convert JSON to JsonArray and manipulate it, like getting the length() ... i can do that directly like this without changing my code: `JsonArray ress = res.get("JSON").getAsJsonArray();`, but getting the same error. – Jis Maxi Apr 26 '17 at 21:26
  • I believe you are looking for `gson.fromJson(jsonString, User.class)`, but that is listed in the Gson documentation, if you take a minute to find it and read though some examples – OneCricketeer Apr 26 '17 at 21:27
  • Are you using Retrofit, by chance? If so, you don't need to remap the JSON back through Gson to convert anything – OneCricketeer Apr 26 '17 at 21:28
  • Yes using Retrofit, return value of error is like this: `res.get("error").getAsString()` , for JSON i believe it will be like this: `res.get("message").getAsJsonArray()`, but return this error `This is not a JSON Array`.. – Jis Maxi Apr 26 '17 at 21:31
  • I don't know what class you have used for the converter, but I assume it's `User` or `List`? If not, why are you manually parsing the raw JSON? – OneCricketeer Apr 26 '17 at 21:32
  • I did that manually without requiring to use User class, User class is used for another activity. – Jis Maxi Apr 26 '17 at 21:34
  • I don't understand the problem, then, sorry. Feel free to edit the question with a [mcve] – OneCricketeer Apr 26 '17 at 21:41
  • Just one note, `.string()` of this line `final String body = response.body().string();` not recognized when i use your example. – Jis Maxi Apr 26 '17 at 21:42
  • If you are using Retrofit or OkHttp `public void onResponse(Call call, Response response)`, it should be. – OneCricketeer Apr 26 '17 at 21:45
  • Yes `public void onResponse(Call call, Response response)`, there high probability that the problem come from the way PHP encoded the json no ? Must decode it first from Java and manipulate it then. – Jis Maxi Apr 26 '17 at 21:47
  • Actually, `response.raw()`, was what I wanted to put – OneCricketeer Apr 26 '17 at 21:48
  • Here how php show the Json result: `$final = [ 'error' => '1', 'json' => json_encode($Json)];`, when json_encode something, the json appear like this: ` [{\"test\":\"1\", \"message\":\"try it\"}, {\"test\":\"2\", \"message\":\"try it\"}, {\"test\":\"3\", \"message\":\"try it\"} ...]` – Jis Maxi Apr 26 '17 at 21:53
  • Your PHP is the issue, yes. Your `$Json` variable is a String, which doesn't need to be encoded. If you would like to encode something, you need an associative array like you made for `$final` and how you encoded that. – OneCricketeer Apr 26 '17 at 21:56
  • (Or you could use a different web server than PHP, if you aren't comfortable with it) – OneCricketeer Apr 26 '17 at 21:57
  • I encoded them well, you can check your self, the PHP code is in the POST, problem is how to decode it on Java. – Jis Maxi Apr 26 '17 at 21:59
  • No, you encoded an already valid JSON string to another JSON String. `"[{\"test\":\"1\"}]"` is not what Gson would like to parse. `[{"test":"1"}]` is – OneCricketeer Apr 26 '17 at 22:01
  • So, i displayed JSON data as string and the JSON is without al the `\"`, `res.get("JSON").getAsString()`, i think i will look for a solution to convert the String to a Json format. – Jis Maxi Apr 26 '17 at 22:05
  • This is your answer. http://stackoverflow.com/questions/6739871/how-to-create-an-array-for-json-using-php – OneCricketeer Apr 26 '17 at 22:06
  • No is not this, you have to understand that my `MAIN JSON` containt two keys: `Error` and `Message`, `Error` is simple String that return 0 or 1, and `message` contain a Datas about another JSON that i will manipulate alone on Java side. For `Error` i can get it without error, but `message` is what we are debugging now. – Jis Maxi Apr 26 '17 at 22:09
  • Same result sir, when i show the json in a log with string format, it's a valid JSON format. – Jis Maxi Apr 26 '17 at 22:22
  • Not a JSON Object: "com.myapp.model.User@99d1185", JSON is decalred as String on user class, what is the right declaration to get it work ? will try to edit it. – Jis Maxi Apr 26 '17 at 22:28