question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONObject transforms to nameValuePair

See original GitHub issue

Example:

class Example {
 @SerializedName("exampleField")
    public JSONObject exampleField = new JSONObject("{\"name\":\"Nick\"}");
}

Log.e(TAG,gson.toJson(new Example()))

result:

"exampleField": {
  "nameValuePairs": {
   "name": "Nick"
  }
 }

expected Result:

"exampleField": {
   "name": "Nick"
   }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
JakeWhartoncommented, Feb 3, 2016

Use com.google.gson.JsonObject not org.json.JSONObject.

5reactions
lingyfhcommented, Sep 7, 2018

GsonBuilder registerTypeAdapter

new GsonBuilder()
.registerTypeAdapter(JSONObject.class, JSONObjectAdapter.sInstance)
.registerTypeAdapter(JSONArray.class, JSONArrayAdapter.sInstance)
    static class JSONObjectAdapter implements JsonSerializer<JSONObject>, JsonDeserializer<JSONObject> {

        public static JSONObjectAdapter sInstance = new JSONObjectAdapter();

        @Override
        public JsonElement serialize(JSONObject src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }

            JsonObject jsonObject = new JsonObject();
            Iterator<String> keys = src.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = src.opt(key);

                JsonElement jsonElement = context.serialize(value, value.getClass());
                jsonObject.add(key, jsonElement);
            }
            return jsonObject;
        }

        @Override
        public JSONObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONObject(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }


    static class JSONArrayAdapter implements JsonSerializer<JSONArray>, JsonDeserializer<JSONArray> {

        public static final JSONArrayAdapter sInstance = new JSONArrayAdapter();

        @Override
        public JsonElement serialize(JSONArray src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            JsonArray jsonArray = new JsonArray();
            for (int i = 0; i < src.length(); i++) {
                Object object = src.opt(i);
                JsonElement jsonElement = context.serialize(object, object.getClass());
                jsonArray.add(jsonElement);
            }
            return jsonArray;
        }

        @Override
        public JSONArray deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return new JSONArray(json.toString());
            } catch (JSONException e) {
                e.printStackTrace();
                throw new JsonParseException(e);
            }
        }
    }

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - How can I delete the nameValuePairs key from the ...
Solution: Pass the actual JAVA object to @Body annotation, not it's corresponding JSONObject. GSON will take care of converting it to JSON representation....
Read more >
Question: How can I transform the properties of a JSON object ...
I have a situation where I need to iterate through the properties of a JSON object and transform them to key/value pairs that...
Read more >
org.apache.http.client.methods.HttpPost.setEntity java code ...
setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request ... Build the JSON object to pass parameters JSONObject jsonObj = new ...
Read more >
Java Code Examples of org.apache.http.message ...
This page provides Java code examples for org.apache.http.message.BasicNameValuePair. The examples are extracted from open source Java projects from GitHub.
Read more >
JSONObject
A JSONObject constructor can be used to convert an external form JSON text into an ... Converts a string to a number using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found