java创建jsonarray,json - 如何使用JSONObj在Java中创建正确的JSONArray

json - 如何使用JSONObj在Java中创建正确的JSONArray

如何使用JSONObject在Java中创建如下所示的JSON对象?

{

"employees": [

{"firstName": "John", "lastName": "Doe"},

{"firstName": "Anna", "lastName": "Smith"},

{"firstName": "Peter", "lastName": "Jones"}

],

"manager": [

{"firstName": "John", "lastName": "Doe"},

{"firstName": "Anna", "lastName": "Smith"},

{"firstName": "Peter", "lastName": "Jones"}

]

}

我找到了很多例子,但不是我的JSONArray字符串。

user2010955 asked 2019-06-28T08:17:21Z

4个解决方案

225 votes

以下是使用java 6开始的一些代码:

JSONObject jo = new JSONObject();

jo.put("firstName", "John");

jo.put("lastName", "Doe");

JSONArray ja = new JSONArray();

ja.put(jo);

JSONObject mainObj = new JSONObject();

mainObj.put("employees", ja);

编辑:由于对put vs add有很多疑惑,我将尝试解释其中的区别。 在java 6中,org.json.JSONArray包含put方法,而在java 7中,javax.json包含add方法。

使用java 7中的构建器模式的示例如下所示:

JsonObject jo = Json.createObjectBuilder()

.add("employees", Json.createArrayBuilder()

.add(Json.createObjectBuilder()

.add("firstName", "John")

.add("lastName", "Doe")))

.build();

Grammin answered 2019-06-28T08:17:51Z

13 votes

我想你是从服务器或文件中获取这个JSON,并且你想从中创建一个JSONArray对象。

String strJSON = ""; // your string goes here

JSONArray jArray = (JSONArray) new JSONTokener(strJSON).nextValue();

// once you get the array, you may check items like

JSONOBject jObject = jArray.getJSONObject(0);

希望这可以帮助 :)

Naeem A. Malik answered 2019-06-28T08:18:18Z

6 votes

可以编写小的可重用方法来创建人json对象以避免重复代码

JSONObject getPerson(String firstName, String lastName){

JSONObject person = new JSONObject();

person .put("firstName", firstName);

person .put("lastName", lastName);

return person ;

}

public JSONObject getJsonResponse(){

JSONArray employees = new JSONArray();

employees.put(getPerson("John","Doe"));

employees.put(getPerson("Anna","Smith"));

employees.put(getPerson("Peter","Jones"));

JSONArray managers = new JSONArray();

managers.put(getPerson("John","Doe"));

managers.put(getPerson("Anna","Smith"));

managers.put(getPerson("Peter","Jones"));

JSONObject response= new JSONObject();

person .put("employees", employees );

person .put("manager", managers );

return response;

}

Manasi answered 2019-06-28T08:18:43Z

1 votes

请尝试这个...希望它有所帮助

JSONObject jsonObj1=null;

JSONObject jsonObj2=null;

JSONArray array=new JSONArray();

JSONArray array2=new JSONArray();

jsonObj1=new JSONObject();

jsonObj2=new JSONObject();

array.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))

.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))

.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));

array2.put(new JSONObject().put("firstName", "John").put("lastName","Doe"))

.put(new JSONObject().put("firstName", "Anna").put("v", "Smith"))

.put(new JSONObject().put("firstName", "Peter").put("v", "Jones"));

jsonObj1.put("employees", array);

jsonObj1.put("manager", array2);

Response response = null;

response = Response.status(Status.OK).entity(jsonObj1.toString()).build();

return response;

MSD answered 2019-06-28T08:19:11Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值