[转帖]Java对象转换为Json对象_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3281 | 回复: 0   主题: [转帖]Java对象转换为Json对象        下一篇 
yuyanzheng
注册用户
等级:下士
经验:176
发帖:9
精华:0
注册:2013-1-31
状态:离线
发送短消息息给yuyanzheng 加好友    发送短消息息给yuyanzheng 发消息
发表于: IP:您无权察看 2013-2-1 13:45:26 | [全部帖] [楼主帖] 楼主

JSON(Javascript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于Javascript Programming Language, Standard ECMA-262 3rd Edition – December 1999的一个子集。JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, Javascript, Perl, Python等)。这些特性使JSON成为理想的数据交换语言。 正是因为这样,twitter已经声称他们的流媒体API将不再支持XML,Foursquare则强烈推荐开发者在使用他们的API时选择JSON,因为他们计划下一代接口只支持JSON。

注*

在进行编码前还需要 commons-beanutils-1.7.0.jar 、 commons-lang-2.5.jar 、commons-logging-1.0.4.jar 、 ezmorph-1.0.6.jar 、 json-lib-1.1-jdk15.jar 、morph-1.1.1.jar

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.ch.entity.JSONTestBean;
import com.ch.entity.Props;
publicclass TestJson {
      publicstaticvoid main(String[] args) {
            TestJson j = new TestJson();
            j.ObjectList2json();
      }
      publicvoid ObjectList2json() {
            Map map = new HashMap();
            List jlist = new ArrayList();
            //JSONTestBean类的构造方法
            JSONTestBean bean1 = new JSONTestBean("zhangbo", "123123");
            JSONTestBean bean2 = new JSONTestBean("lisi", "65489");
            //Props类的构造方法
            Props props = new Props("127.0.0.1", "8008");
            jlist.add(bean1);
            jlist.add(bean2);
            map.put("Props", props);
            map.put("jsonObjectList", jlist);
            JSONArray jsonArray = JSONArray.fromObject(map);
            System.out.println(jsonArray);
      }
      publicvoid arr2json() {
      boolean[] boolArray = newboolean[] { true, false, true };
            JSONArray jsonArray = JSONArray.fromObject(boolArray);
            System.out.println(jsonArray);
            // prints [true,false,true]
      }
      publicvoid list2json() {
            List list = new ArrayList();
            list.add("first");
            list.add("second");
            JSONArray jsonArray = JSONArray.fromObject(list);
            System.out.println(jsonArray);
            // prints ["first","second"]
      }
      publicvoid createJson() {
            JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
            System.out.println(jsonArray);
            // prints ["json","is","easy"]
      }
      publicvoid map2json() {
            Map map = new HashMap();
            map.put("name", "json");
            map.put("bool", Boolean.TRUE);
            map.put("int", new Integer(1));
      map.put("arr", new String[] { "a", "b" });
      map.put("func", "function(i){ return this.arr[i]; }");
            JSONObject json = JSONObject.fromObject(map);
            System.out.println(json);
            // prints
            // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
            // return this.arr[i]; }]
      }
      publicvoid bean2json() {
            JSONObject jsonObject = JSONObject.fromObject(new JSONTestBean(
            "zhangbo", "234234"));
            System.out.println(jsonObject);
      }
      publicvoid json2bean() {
      String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}";
                  // JSONObject jb = JSONObject.fromString(json);
                  // JSONObject.toBean(jb, MyBean.class);
                  System.out.println(json);
            }
      }




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论