How to convert list data into json in java

前端 未结 7 1211
天涯浪人
天涯浪人 2020-11-30 06:57

I have a function which is returning Data as List in java class. Now as per my need, I have to convert it into Json Format.

Below is my fun

7条回答
  •  不知归路
    2020-11-30 07:23

    JSONObject responseDetailsJson = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    
    List ls =new  ArrayList();
    
    for(product cj:cities.getList()) {
        ls.add(cj);
        JSONObject formDetailsJson = new JSONObject();
        formDetailsJson.put("id", cj.id);
        formDetailsJson.put("name", cj.name);
        jsonArray.put(formDetailsJson);
    }
    
    responseDetailsJson.put("Cities", jsonArray);
    
    return responseDetailsJson;
    

提交回复
热议问题