In java, you need to initialize the class values ​​from One and Two or the same from AAA

public final class setting{ boolean flag = true; String one; long two; } 

JsonObject response contains

 { "One": "text", "Two": 111111, "AAA": {"One": "text2", "Two": 222222} } 

    1 answer 1

    1. Plug in gson
      compile 'com.google.code.gson:gson:2.8.0' 
    1. Correct the class so that the field names match the JSON field names (or use annotations to specify the field names)
     public class Setting { @SerializedName("One") String one; @SerializedName("Two") long two; } 
    1. Create a class instance from JSON like this:
     String json = "{\"One\":\"text2\", \"Two\":222222}" GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Setting setting = gson.fromJson(json , Setting.class); 
    • The object already has a JsonObject response and in square brackets its contents, depending on the flag, let's say boolean two = true choose values ​​from AAA and if the flag is false, then you don’t need to touch AAA if there is a response (containsKey ("One")) .one = response.getJsonNumber ("One"). longValue (); How to make AAA too - S. Oleg
    • @ S.Oleg, according to this validator, you don’t have JSON jsonlint.com. If there was a regular JSON, then you could parse it with a gson into two objects, one is nested into the other and then do anything you like with them. So far, the essence of your difficulties is not clear - YuriySPb