java利用json-lib操作json,javajson-libjson

來源:互聯網
上載者:User

java利用json-lib操作json,javajson-libjson
1.1.  下載json-lib.jar

http://sourceforge.net/projects/json-lib/files/json-lib/

1.2.  Java對象轉換為json1.2.1.  Map對象轉換為json

Map2Json.java

package json;

 

import java.util.HashMap;

import java.util.Map;

 

import net.sf.json.JSONArray;

 

public class Map2Json {

 

    public static void main(String[] args) {

      

       Map<String, String> map = new HashMap<String, String>();

       map.put("姓名","張三");

       map.put("年齡","22");

       map.put("性別","男");

      

       JSONArray jsonArray = JSONArray.fromObject(map);

      

       System.out.println(jsonArray.toString());

 

    }

 

}

 

運行結果:

[{"性別":"男","姓名":"張三","年齡":"22"}]

1.2.2.  List轉換為json對象

List2json.java

package json;

 

import java.util.ArrayList;

import java.util.List;

 

import net.sf.json.JSONArray;

 

public class List2Json {

 

         public static void main(String[] args) {

                  

                   List<String> list = new ArrayList<String>();

                  

                   list.add("aa");

                   list.add("bb");

                   list.add("cc");

                   list.add("dd");

                  

                   JSONArray jsonArray = JSONArray.fromObject(list);

                  

                   System.out.println(jsonArray.toString());

 

         }

 

}

 

結果:

["aa","bb","cc","dd"]

1.2.3.  Java bean轉為json對象

Person.java

package json;

 

import java.util.Date;

 

public class Person {

   

    private Stringname;

   

    private Stringsex;

   

    private int age;

   

    private Stringaddress;

   

    private Datebirthday;

 

    public String getName() {

       returnname;

    }

 

    public void setName(String name) {

       this.name = name;

    }

 

    public String getSex() {

       returnsex;

    }

 

    public void setSex(String sex) {

       this.sex = sex;

    }

 

    public int getAge() {

       returnage;

    }

 

    public void setAge(int age) {

       this.age = age;

    }

 

    public String getAddress() {

       returnaddress;

    }

 

    public void setAddress(String address) {

       this.address = address;

    }

 

    public Date getBirthday() {

       returnbirthday;

    }

 

    public void setBirthday(Date birthday) {

       this.birthday = birthday;

    }

 

    public Person(String name, String sex,int age, String address,

           Date birthday) {

       super();

       this.name = name;

       this.sex = sex;

       this.age = age;

       this.address = address;

       this.birthday = birthday;

    }

 

    public Person() {

       super();

       // TODO Auto-generated constructor stub

    }

   

   

 

}

 

Bean2Json.java

package json;

 

import java.util.Date;

 

import net.sf.json.JSONArray;

 

public class Bean2Json {

 

   public static void main(String[] args) {

     

      Person person = new Person();

      person.setAddress("深圳福田");

      person.setAge(22);

      person.setBirthday(new Date());

      person.setName("張三");

      person.setSex("男");

     

      JSONArray jsonArray = JSONArray.fromObject(person);

     

      System.out.println(jsonArray.toString());

 

   }

 

}

運行結果:

[{"address":"深圳福田","age":22,"birthday":{"date":27,"day":2,"hours":22,"minutes":47,"month":0,"seconds":22,"time":1422370042957,"timezoneOffset":-480,"year":115},"name":"張三","sex":"男"}]

1.3.  Json轉換為map對象

Json2Map.java

package json;

 

import java.util.HashMap;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

 

import net.sf.json.JSONObject;

 

public class Json2Map {

 

    public static void main(String[] args) {

      

       String jsonStr = "{'性別':'男','姓名':'張三','年齡':'22'}";

      

       JSONObject object = JSONObject.fromObject(jsonStr);

      

       Map<String,String> map = new HashMap<String,String>();

      

       Set<String> keySet = object.keySet();

      

       for (String key : keySet) {

           map.put(key, object.getString(key));

       }

      

       Set<Entry<String,String>> entrySet = map.entrySet();

      

       for (Entry<String, String> entry : entrySet) {

           System.out.println(entry.getKey()+":"+entry.getValue());

       }

      

    }

 

}

 

運行結果:

性別:男

姓名:張三

年齡:22

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.