1

I wish to get the JSON output as below which is two objects combined I think without using array. How can I get the following output?

{
    "AppData": {
        "status": "****",
        "message": [
            ""
        ]
    },
    "Data": {
        "token": "****"
    }
}

I try the array way and if I get rid of the "[" and "]" then it will become invalid JSON.

My code as follows

public Response getSAppData(int id, String email, String password){
    Map<String, AppData> AppDataHM = new HashMap<String, AppData>(); 
    Map<String, Data> DataHM1 = new HashMap<String, Data>();

    Data data = DataHM.get(new AppDataRequest (id, email, password));
    List<String> message = new ArrayList<>();

    message.add("");
    AppDataHM.put("AppData", new AppData("success", message));
    DataHM1.put("Data", data);
    String AppDataJO = new Gson().toJson(AppDataHM);
    String DataJO = new Gson().toJson(DataHM1);
    String ADJODJOA = "["+AppDataJO+","+DataJO+"]"; 
    return Response.status(200).entity(ADJODJOA).build();
}

And the output of my code as below

[
  {
    "AppData": {
      "status": "success",
      "message": [
        ""
      ]
    }
  },
  {
    "Data": {
      "token": "token1"
    }
  }
]

Thank in advance for any reply and answer.

1 Answer 1

1

All. I find a solution by first combining the two HashMap into one then only convert it to object. Below is my code.

Map<String, AppData> AppDataHM = new HashMap<String, AppData>(); 
Map<String, Data> DataHM1 = new HashMap<String, Data>();

Map ADHMDHM = new HashMap<>();

Data data = DataHM.get(new AppDataRequest (id, email, password));
List<String> message = new ArrayList<>();

message.add("");
AppDataHM.put("AppData", new AppData("success", message));
DataHM1.put("Data", data);
ADHMDHM.putAll(AppDataHM);
ADHMDHM.putAll(DataHM1);
String ADHMDHM1 = new Gson().toJson(ADHMDHM);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.