Android中生成json格式数据的几种方法
2017-08-07 16:14
579 查看
String生成的json数据有两种
1String json="[{'name':'tom','age':12},{'name':'jack','age':13}]"; JSONArray a = new JSONArray(json); System.out.println(a.toString());
2
String json2="{'json':[{'name':'tom','age':14},{'name':'jack','age':15}]}"; JSONObject b= new JSONObject(json2); System.out.println(b.toString());
String类型的数据加“”,然后每个key,value加单引号。
JsonObject生成的数据有两种
1JSONArray array =new JSONArray(); JSONObject object =new JSONObject(); JSONObject object1 =new JSONObject(); JSONObject obj= new JSONObject(); try { object.put("item1","value1"); object.put("age",12); object.put("name","tom"); object1.put("item2","value2"); object1.put("age",12232); object1.put("name","tom"); array.put(object); array.put(object1); obj.put("name",array); System.out.println(obj.toString()); }catch (Exception e){ } 结果:{"name":[{"item1":"value1","name":"tom","age":12},{"item2":"value2","name":"tom","age":12232}]}
2
JSONArray array1 =new JSONArray(); JSONObject object2 =new JSONObject(); JSONObject object3 =new JSONObject(); try { object2.put("color","red"); object2.put("height",20); object3.put("color","blue"); object3.put("height",1010); array1.put(object2); array1.put(object3); System.out.println(array1.toString()); }catch (Exception e){ } 结果:[{"color":"red","height":20},{"color":"blue","height":1010}]
将集合生成json数据
1Map<String ,String> map =new HashMap<>(); Map<String ,String> map2 =new HashMap<>(); map.put("name1","tom1"); map.put("age1","12"); map2.put("name1","tom1"); map2.put("age1","12"); JSONObject object4 =new JSONObject(); JSONArray array2 =new JSONArray(); array2.put(map); array2.put(map2); object4.put("key",array2); System.out.println(object4.toString()); 结果:{"key":[{"name1":"tom1","age1":"12"},{"name1":"tom1","age1":"12"}]}
2
Map<String ,String> map1 =new HashMap<>(); map1.put("as","adasd"); map1.put("asfa","afasff"); JSONArray array3 =new JSONArray(); array3.put(map1); System.out.println(array3.toString()); 结果:[{"asfa":"afasff","as":"adasd"}]
基本都是类似。。
相关文章推荐
- Android--解析Json格式数据的几种方法
- Android App数据格式Json解析方法和常见问题
- Android编程解析Json格式数据的方法
- Java Servlet生成JSON格式数据并用jQuery显示的方法
- Json数据格式介绍、使用Android自带类库完成Json生成与解析
- Android App数据格式Json解析方法和常见问题
- Android基础之json数据的几种解析方法
- Android App数据格式Json解析方法和常见问题
- Android中解析JSON格式数据常见方法合集
- Android中的httpClient使用及JSON格式数据的解析方法
- Android解析相同接口返回不同格式json数据的方法
- Android解析json数据的几种方法
- 【原创】将数据生成json文件下载以及json中文乱码和优化json文件格式的方法
- Android App中读取XML与JSON格式数据的基本方法示例
- Java Servlet生成JSON格式数据并用jQuery显示的方法
- Json数据格式介绍、使用Android自带类库完成Json生成与解析
- 【Android开发经验】Json数据格式介绍、使用Android自带类库完成Json生成与解析
- Android中解析JSON格式数据常见方法合集
- JSON学习(案列):Android中解析JSON格式数据常见方法合集
- Json数据格式介绍、使用Android自带类库完成Json生成与解析