json学习(一)--将一个对象或集合转换成符合json数据格式的字符串
2012-12-19 20:15
1061 查看
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]}
有如下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]}
相关文章推荐
- 后台将对象转换为json格式,前台js解析json数据,将字符串转化为数组
- 如何将一个List<Map<String,String>>类型的集合数据转换成json格式输出
- 如何将C#/.NET 将json字符串格式数据转换成对象
- Newtonsoft将对象或对象集合转换成JSON数据时,日期格式的设置
- 如何将一个java对象(包括java对象数组、集合)转换成一个json字符串
- JSON(1):如何将符合json语法的字符串转换为一个JS对象
- JSON 格式的转换: 数组、字符串、List集合、DataTable,实体对象
- 将集合属性对象转换成json格式字符串
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- android/java中把对象、对象bean、Collection以及字符串转换成Json格式数据
- 将一个对象集合转换成JSON格式
- json的作用:按规定格式保存js语言中的一组数据并转换为字符串(json是种特殊的容器,一般保存对象和数组数据(可以嵌套),并把它们装换为字符串在网络中传输?))
- c# json数据解析——将字符串json格式数据转换成对象
- c# json数据解析——将字符串json格式数据转换成对象
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- JSON(2):如何将一个java对象转换为一个符合json语法的字符串
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- JSON数据转换为字符串对象
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
- 使用Javascript/jQuery将javascript对象转换为json格式数据