且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在java中正确格式化JSON字符串?

更新时间:2023-11-10 09:40:58

您可以使用 json-lib 。这很方便!你可以像这样构造你的json字符串:

you can use json-lib. it's very convenient! you can construct your json string like this:

JSONObject dataSet = new JSONObject();
dataSet.put("keyA", "SomeValueA") ;
dataSet.put("keyB", "SomeValueB") ;
dataSet.put("keyC", "SomeValueC") ;

JSONObject someProperty = new JSONObject();
dataSet.put("someproperty", "aproperty") ;

JSONArray properties = new JSONArray();
properties.add(dataSet);
properties.add(someProperty);

当然你只需调用 properties.toString即可获得你的JSON字符串()