Android - Gson upgrade - how to parse a JSON String into an object with a JSON key in a JSON response?

This is my JSON answer:

{
      "id": 2,
      "name": "Test",
      "content": "{\"type\": \"status\", \"text\": \"Lorem ipsum dummy text.\", \"id\": 1}"
}

These are model structures:

class TestModel {
    public int id;
    public String name;
    public Content content;
}

class Content {
    public int id;
    public String status;
    public String text;
}

I want to analyze the meaning of content directly in my content model object using Retrofit and GsonConvertor. But I'm currently parsing it as a String value, than by converting Gson.fromJson () to a content model object. Is there any solution to get the expected result?

When I used it for analysis using the GsonConverterFactory, Retrofit gives a callback in the onFailure method with this exception:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 4 column 19 path $.data[0].content
+4
source share
3 answers

JSON, JSON. "content" , :

{
    "id": 2,
    "name": "Test",
    "content": {
        "type": "status",
        "text": "Lorem ipsum dummy text.",
        "id": 1
    }
}

gson.fromJson(response, TestModel.class), RetroFit GsonConverterFactory .


, , JSON, . , , , , , . , , content TestModel String:

class TestModel {
    public int id;
    public String name;
    public String content;
}

class Content {
    public int id;
    public String type;
    public String text;
}

:

TestModel testModel = gson.fromJson(response, TestModel.class);
Content content = gson.fromJson(testModel.content, Content.class);

, TypeAdapter content:

public class ContentAdapter extends TypeAdapter<Content> {

    @Override
    public void write(JsonWriter out, Content value) throws IOException {
        // TODO: Writer implementation
    }

    @Override
    public Content read(JsonReader in) throws IOException {
        if(in.peek() != JsonToken.NULL) {
            return fromJson(in.nextString());
        } else {
            in.nextNull();
            return null;
        }
    }

}

TypeAdapter GSON:

Gson gson = new GsonBuilder()
        .registerTypeAdapter(Content.class, new ContentAdapter()).create();
+3

, 2.1.0, , Gson . , . converterFactory GsonConverterFactory, .

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

Gson json.

. .

gradle

compile 'com.squareup.retrofit2:converter-gson:2.0.2'
0
    import java.util.ArrayList;
    import java.util.List;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class Parser {
        public static List<TestModel> JsonParser(String JsonString) {
            try {
                JSONArray array = new JSONArray(content);
                List<TestModel > arrayList = new ArrayList();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    TestModel  model = new TestModel();
                    model.setId(Integer.parseInt(obj.getString("id")));
                    model.setName(obj.getString("name"));


    JSONArray contentArray = newJSONArray(obj.getString("content")));
                        JSONObject obj1 = contentArray.getJSONObject(1);
                        Content content = new Content();

                        content.setId(Integer.parseInt(obj1.getString("id")));
                        content.setStatus(obj1.getString("status"));
                        content.setText(obj1.getString("text"));


                    model.setContent(content);
                    arrayList.add(model);
                }
                return arrayList;
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
   class TestModel {
//Generate Setter Getter for all properties
    public int id;
    public String name;
    public Content content;
}

class Content {
//Generate Setter Getter for all properties
    public int id;
    public String status;
    public String text;
}
-2
source

Source: https://habr.com/ru/post/1653753/


All Articles