JSONObject和JSONArray
来源:互联网 发布:led电子屏软件 编辑:程序博客网 时间:2024/06/01 18:34
package com.test;import java.util.ArrayList;import java.util.List;import org.junit.Test;import net.sf.json.JSONArray;import net.sf.json.JSONObject;/** * * @author Cherry * @date 2017年10月16日 * */public class JsonTest { /** * Java Object to Json */ @Test public void show() { List<Music> list = new ArrayList<Music>(); for (int i = 0; i < 10; i++) { Music m = new Music(i, "海韵" + i, "邓丽君"); list.add(m); } // Object to Json JSONObject js = JSONObject.fromObject(new Music("海韵", "邓丽君")); System.out.println(js.toString()); System.out.println(js.getInt("id") + "-->" + js.getString("name") + "-->" + js.getString("singer")); ; // List to Json JSONArray ja = JSONArray.fromObject(list); System.out.println(ja.toString()); System.out.println(ja.get(0)); } /** * add content to Json */ @Test public void show2() { JSONObject js = new JSONObject(); js.put(1, new Music(30, "海韵", "邓丽君")); js.put(2, new Student(30, "Chen", "chen")); System.out.println(js.toString()); System.out.println(js.get("1")); }}
json-lib需要commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.5.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar五个包的支持
show()执行的结果
{"id":0,"name":"海韵","singer":"邓丽君"}0-->海韵-->邓丽君[{"id":0,"name":"海韵0","singer":"邓丽君"},{"id":1,"name":"海韵1","singer":"邓丽君"},{"id":2,"name":"海韵2","singer":"邓丽君"},{"id":3,"name":"海韵3","singer":"邓丽君"},{"id":4,"name":"海韵4","singer":"邓丽君"},{"id":5,"name":"海韵5","singer":"邓丽君"},{"id":6,"name":"海韵6","singer":"邓丽君"},{"id":7,"name":"海韵7","singer":"邓丽君"},{"id":8,"name":"海韵8","singer":"邓丽君"},{"id":9,"name":"海韵9","singer":"邓丽君"}]{"id":0,"name":"海韵0","singer":"邓丽君"}
show2()执行的结果
{"1":{"id":30,"name":"海韵","singer":"邓丽君"},"2":{"id":30,"name":"Chen","pass":"chen"}}{"id":30,"name":"海韵","singer":"邓丽君"}
阅读全文
0 0
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONArray和JSONObject
- JSONArray和JSONObject
- 关于JSONObject和JSONArray
- 使用JSONObject和JSONArray
- JSONObject 和 JSONArray
- JSONObject 和 JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray
- JSONObject和JSONArray使用
- JSONObject,JSON,和JSONArray
- jsonObject和jsonArray
- JsonObject和JsonArray解析
- JSONObject和JSONArray详解
- JSONObject和JSONArray使用
- List<JSONObject>和JSONArray
- MYSQL实现将两个结果集合并,并且按照时间字段分组,其他字段的值求和
- POJ-3067 Japan (树状数组)
- 详解重写equals()方法就必须重写hashCode()方法的原因
- android 打包知识
- PixHawk学习笔记 之 源码浅析—mc_pos_control.cpp—do_control(dt)—control_non_manual(dt)
- JSONObject和JSONArray
- 求1!+2!+3!+……+20!的和
- 28、C#里面的单选框RadioButton的属性和事件
- 大数据竞赛平台——Kaggle 入门
- spring quartz 的用法
- 常用git命令
- 【正一专栏】曼城攻击力惊人露出冠军相
- Java 线程的6种状态
- web项目不能部署到本地tomcat?