将JSON数组附加到JSONObject

时间:2016-08-06 04:25:17

标签: java json jsonobject

    shingles, shingleStarter, ridgeCap = getshingleCalcs(length, roofArea, rakeLength)

我如何追加:

JSONObject jsonObj = new JSONObject("string");
jsonObj.toString();

"employees":[{"firstname":"stack","lastname":"overflow"}, {"firstname":"Happy","lastname":"Coding"}]

以上json对象

我想要

{"firstname":"Gloomy","lastname":"Sunday"} 

2 个答案:

答案 0 :(得分:0)

请先自己做研究,这个网站不是为你做计算机科学的功课。因为我需要一些代表...

public class ex{
    void example(){
        try{
            JSONObject jobj = new JSONObject("[{firstname:stack,lastname:overflow}]");
            JSONObject jobj2 = new JSONObject("[{firstname:Happy,lastname:Coding}]");
            JSONArray jarray = new JSONArray();
            jarray.put(jobj);
            jarray.put(jobj2);
        }catch(JSONException e){
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

JSONObject jsonObj = new JSONObject();
JSONObject jo1 = new JSONObject("{"firstname":"Gloomy","lastname":"Sunday"}");
jsonObj.append("employees", jo1);