json学习(一)--将一个对象或集合转换成符合json数据格式的字符串

json主要用于传送数据,主要格式如下:{“name“:"value"}

有如下xml文件

  • <section>
     <title>Book-Singing Event</title>
     <signing>
      <author title="Mr" name="Vikram Seth"/>
      <book title="A Suitable Boy" price="$22.95"/>
     </signing>
     <signing>
      <author title="Dr" name="Oliver Sacks"/>
      <book title="The Island of the Color-Blind" price="$1295"/>
     </signing>
    </section>

将其转换成javascript中的json数据如下:

  • <script type="text/javascript">
     function check()
     {
      var info = {"section":{
          "title":"Book-Singing Event",
          "singing":[{
           "author":{"title":"Mr","name":"Vikram Seth"},
           "book":{"title":"A Suitable Boy","price":"$22.95"}
          },{
           "author":{"title":"Dr","name":"Oliver Stacks"},
           "book":{"title":"The Island of Color-Blind", "price":"$12.95"}
          }]
       }};
      alert(info.section.title);
      alert(info.section.singing[0].book.title);
     }
    </script>

     将会弹出提示框:Book-Singin Event和A Suitable Boy

 

json in java 源码下载地址:https://github.com/douglascrockford/JSON-java点击图标ZIP

下载完成后,将源码复制到org.json包下

编写一个类使用JSONObject类和JSONArray

package com.json.study;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
 public static void main(String[] args) throws JSONException {
  //假设这个字符串从客户端传来
  String jsonContext = "{'hello':'world','abc':'xyz'}";
  //抛异常,json数据的格式可能不正确
  JSONObject jsonObject = new JSONObject(jsonContext);
  String str1 = jsonObject.getString("hello");
  String str2 = jsonObject.getString("abc");
  
  System.out.println(str1);
  System.out.println(str2);
  
  System.out.println("---------------------------");
  
  jsonContext = "[{'hello':333,'abc':false, 'xyz':'test'},{'hello':555,'abc':true,'xyz':'test'}]";
  JSONArray jsonArray = new JSONArray(jsonContext);
  System.out.println(jsonArray.length());
  for(int i = 0; i < jsonArray.length(); i++)
  {
   JSONObject jsonObject2 = jsonArray.getJSONObject(i);
   int value1 = jsonObject2.getInt("hello");
   boolean value2 = jsonObject2.getBoolean("abc");
   String value3 = jsonObject2.getString("xyz");
   System.out.println(value1+"   "+value2+"   "+value3);  
  }
 }
}

 执行结果:

world
xyz
---------------------------
2
333   false   test
555   true   test

 

 

将一个对象(bean)转换成可以符合json数据格式的字符串,需要gson.jar包,下载地址:http://code.google.com/p/google-gson/

    Person类:

ackage com.json.study;

import java.util.List;

public class Person {
 private String username;
 private String password;
 private int age;
 private String address;
 private List list;
 public Person() {
  super();
 }
 
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }

 public List<String> getList() {
  return list;
 }

 public void setList(List<String> list) {
  this.list = list;
 }
 
}

转换方式如下:

package com.json.study;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class Test2 {
 public static void main(String[] args) {
  Person person = new Person();
  person.setUsername("zhangsan");
  person.setPassword("123456");
  person.setAddress("beijing");
  person.setAge(30);
  
  List list = new ArrayList();
  list.add(new String("abc"));
  list.add(false);
  
  person.setList(list);
  Gson gson = new Gson();
  //将一个对象转换成符合json格式的字符串
  String result = gson.toJson(person);
  System.out.println(result);
  
  
 }
}
结果:

{"username":"zhangsan","password":"123456","age":30,"address":"beijing","list":["abc",false]}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值