• java对象与json对象间的相互转换


    工程中所需的jar包,因为在网上不太好找,所以我将它放到我的网盘里了,如有需要随便下载。

    点击下载

    1.简单的解析json字符串

    首先将json字符串转换为json对象,然后再解析json对象,过程如下。
    JSONObject jsonObject = JSONObject.fromObject(jsonStr);
    [java] view plain copy
    1. <pre></pre><span style="white-space:pre"></span>  
    2. <pre></pre>  
      根据json中的键得到它的值
    String name = jsonObject.getString("name");
    int num = jsonObject.getInt("num");
    String sex = jsonObject.getString("sex");
    int age = jsonObject.getInt("age");

    2.将json字符串转换为java对象

    同样先将json字符串转换为json对象,再将json对象转换为java对象,如下所示。
    JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
    将json对象转换为java对象
    Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象

    3.将java对象转换为json字符串

    先将java对象转换为json对象,在将json对象转换为json字符串
    JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
    String str = json.toString();//将json对象转换为字符串
    完整代码如下:

    [java] view plain copy
    1. package baz.parse;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import net.sf.json.JSON;  
    7. import net.sf.json.JSONArray;  
    8. import net.sf.json.JSONObject;  
    9. import net.sf.json.JSONSerializer;  
    10. import baz.bean.Person;  
    11.   
    12. public class ParseJson {  
    13.       
    14.     private String jsonStr;  
    15.       
    16.     public ParseJson() {  
    17.           
    18.     }  
    19.       
    20.     public ParseJson(String str){  
    21.         this.jsonStr = str;  
    22.     }  
    23.     /** 
    24.      * 解析json字符串 
    25.      */  
    26.     public void parse(){  
    27.         JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
    28.         String name = jsonObject.getString("name");  
    29.         int num = jsonObject.getInt("num");  
    30.         String sex = jsonObject.getString("sex");  
    31.         int age = jsonObject.getInt("age");  
    32.           
    33.         System.out.println(name + " " + num + " " + sex + " " + age);  
    34.     }  
    35.     //将json字符串转换为java对象  
    36.     public Person JSON2Object(){  
    37.         //接收{}对象,此处接收数组对象会有异常  
    38.         if(jsonStr.indexOf("[") != -1){  
    39.             jsonStr = jsonStr.replace("[", "");  
    40.         }  
    41.         if(jsonStr.indexOf("]") != -1){  
    42.             jsonStr = jsonStr.replace("]", "");  
    43.         }  
    44.         JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象  
    45.         Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象  
    46.         return jb;//返回一个Person对象  
    47.     }  
    48.       
    49.   
    50. }  


    [java] view plain copy
    1. package baz.bean;  
    2.   
    3. public class Person {  
    4.       
    5.     private String name;  
    6.     private int num;  
    7.     private String sex;  
    8.     private int age;  
    9.       
    10.     public Person() {  
    11.         // TODO Auto-generated constructor stub  
    12.     }  
    13.   
    14.     public Person(String name, int num, String sex, int age) {  
    15.         super();  
    16.         this.name = name;  
    17.         this.num = num;  
    18.         this.sex = sex;  
    19.         this.age = age;  
    20.     }  
    21.   
    22.   
    23.   
    24.     public String getName() {  
    25.         return name;  
    26.     }  
    27.   
    28.     public void setName(String name) {  
    29.         this.name = name;  
    30.     }  
    31.   
    32.     public int getNum() {  
    33.         return num;  
    34.     }  
    35.   
    36.     public void setNum(int num) {  
    37.         this.num = num;  
    38.     }  
    39.   
    40.     public String getSex() {  
    41.         return sex;  
    42.     }  
    43.   
    44.     public void setSex(String sex) {  
    45.         this.sex = sex;  
    46.     }  
    47.   
    48.     public int getAge() {  
    49.         return age;  
    50.     }  
    51.   
    52.     public void setAge(int age) {  
    53.         this.age = age;  
    54.     }  
    55.       
    56. }  




     将java对象转换为json字符串
    [java] view plain copy
    1. package baz.cons;  
    2.   
    3.   
    4. import net.sf.json.JSONObject;  
    5.   
    6.   
    7. /** 
    8.  * 将java对象转换为json字符串 
    9.  * @author Administrator 
    10.  * 
    11.  */  
    12. public class ConsJson {  
    13.       
    14.     public ConsJson() {  
    15.         // TODO Auto-generated constructor stub  
    16.     }  
    17.       
    18.     public String Object2Json(Object obj){  
    19.         JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象  
    20.         String str = json.toString();//将json对象转换为字符串  
    21.           
    22.         return str;  
    23.     }  
    24. }  

    测试类:
    [java] view plain copy
    1. package baz.test;  
    2.   
    3. import java.util.List;  
    4.   
    5. import baz.bean.Person;  
    6. import baz.cons.ConsJson;  
    7. import baz.parse.ParseJson;  
    8.   
    9.   
    10. public class Test {  
    11.     public static void main(String[] args) {  
    12.           
    13.         //将字符串转换为json对象,然后根据建得到相应的值  
    14.         ParseJson pj = new ParseJson("{"name":"gu","num":123456,"sex":"male","age":24}");  
    15.         pj.parse();  
    16.           
    17.         //将一个json字符串转换为java对象  
    18.         Person p = pj.JSON2Object();  
    19.         System.out.println("Name:" + p.getName());  
    20.         System.out.println("Num:" + p.getNum());  
    21.         System.out.println("Sex:" + p.getSex());  
    22.         System.out.println("age:" + p.getAge());  
    23.           
    24.         //将一个java对象转换为Json字符串  
    25.         Person p1 = new Person("gu1",123,"male",23);  
    26.         ConsJson cj = new ConsJson();  
    27.         String str1 = cj.Object2Json(p1);  
    28.         System.out.println(str1);  
    29.           
    30.     }  
    31.   
    32. }  
    测试输出如下:
    gu 123456 male 24
    Name:gu
    Num:123456
    Sex:male
    age:24
    {"age":23,"name":"gu1","num":123,"sex":"male"}
    这只是最简单使用方法,其他的使用我会在后期中更新。
    我只是初学者,欢迎大侠拍砖!!


  • 相关阅读:
    [转]android Intent机制详解
    [转]Android进程与线程基本知识
    HTML背景图片自适应
    边框边界填充理解
    [转]Android 代码自动提示功能
    [转]Windows7:Visual Studio 2008试用版的评估期已经结束解决方法
    eclipse安装、汉化、搭建安卓开发环境
    asp.net控件拖不动。控件错误
    opengl 入门浅学(一)
    opengl 无法定位程序输入点_glutInitWithExit于动态链接库glut32.dll上
  • 原文地址:https://www.cnblogs.com/jym-sunshine/p/5292512.html
Copyright © 2020-2023  润新知