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]}