Reputation: 5830
I am using Volley with GSON and I need to send a object as a parameter to my call.
This is how I do the object:
JSONObject params = new JSONObject();
Gson gson = new Gson();
String json = gson.toJson(route);
params.put("route", json);
And then I call my Volley JsonObjectRequest function.
The problem is that the params look like this:
{"route":"{\"bounds\":{\"northeast\":{\"lat\":52.3777194,\"lng\":4.924666999999999},\"southwest\":{\"lat\":52.36881109999999,\"lng\":4.9011479}},\"copyrights\":\"Map data ©2014 Google\", etc...}"
As you can see, instead of sending it as a object, its sending it as a String , and that's why I get the " before the {} (before the object begins). The params should look like:
{"route":{\"bounds\":{\"northeast\":{\"lat\":52.3777194,\"lng\":4.924666999999999},\"southwest\":{\"lat\":52.36881109999999,\"lng\":4.9011479}},\"copyrights\":\"Map data ©2014 Google\", etc...}
So no " before { like this:
{"route":{myObject}
What an I doing wrong here?
Upvotes: 0
Views: 114
Reputation: 39386
You don't want to mix JSONObject
and GSON
.
That's 2 different libraries.
Use gson.toJsonTree
to obtain an element, then use JsonObject
instead of JSONObject
:
JsonObject params = new JsonObject();
Gson gson = new Gson();
params.add("route", gson.toJsonTree(route));
Upvotes: 1