1-Json 类似{'name':'luxury','age':'22','company':'esoon'}形式的结构,用于传输数据。此外还有 xml格式的传输格式;但是,在大部分的项目中 大家还是选择了json形式。就在于它的解析和使用起来方便; 我现在学习的项目中就是从微博开放平台获取微博用户的信息,获取过来的是json格式,好像微博开发API提供的只有json格式,腾讯开发平台的API提供XML和Json两种返回格式;XML解析我喜欢用dom4j,跑题了...
可以将字符串,对象,数组等转为json格式,进行传输;
例举一些我的小Demo:
package com.bdc2;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;
import com.bdc.Student;
public class TestJson {
/**
* json对象 与 java对象 转换
*/
@Test
public void test1(){
//创建 Studnet 对象 -> s1
Student s1 = new Student();
s1.setAge(22);
s1.setName("fuck");
s1.setScore(91.0);
//将 s1 对象 放入 list
List<Student> list = new ArrayList<Student>();
list.add(s1);
System.out.println("************************************");
//将 list转为 JSONArray
JSONArray array = JSONArray.fromObject(list);
System.out.println(array.toString());
System.out.println("************************************");
//将 s1 转为 JSONArray
JSONArray array2 = JSONArray.fromObject(s1);
System.out.println(array2.toString());
System.out.println("************************************");
//将 s1 转为 JSONObject
JSONObject object = JSONObject.fromObject(s1);
System.out.println(object);
System.out.println("************************************");
//将 object 转回为 Student 对象
Student student = (Student) JSONObject.toBean(object, Student.class);
System.out.println("Student: "+student.getAge());
System.out.println("************************************");
//将 JSONArray 转为 list
List<Student> list2 = JSONArray.toList(array2);
System.out.println(list2);
}
/**
* 创建一个 JSONObject
* 并 得到 json 里的数据
*/
@Test
public void test2(){
JSONObject object = new JSONObject();
object.put(1, "hao");
object.put(2, "good");
object.put(3, "544");
object.put(4, "you");
System.out.println(object.getInt("3"));
System.out.println(object.getString("4"));
}
/**
* 解析一个json 文件来源 - testjson.txt
* 文件内容:
* {"name":"luxury","age":"24","from":"Beijing","birthday":"1991-10-29
19:00","go":[{"GouBangZi":"love","anhui":"home"}],"for":"some love"}
* @throws IOException
*/
@Test
public void test3() throws IOException{
//将文件内容读入文件流
FileInputStream fis = new FileInputStream("R:\\testjson.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = "";
String line = "";
//读取内容
while((line=br.readLine())!=null){
result = result + line ;
}
fis.close();
br.close();
//创建 JSONObject fromObject:从文件读取的内容
JSONObject obj = JSONObject.fromObject(result);
System.out.println(obj.toString());
System.out.println(obj.getString("from"));
System.out.println("**************************");
//jsonArray 里 还有一个 jsonObject
JSONArray arr = obj.getJSONArray("go");
System.out.println("arr: "+arr);
System.out.println(arr.getJSONObject(0));
}
/**
* 一般数组 转化为 json
*/
@Test
public void test4(){
boolean[] boo = new boolean[]{false,true,false,true};
// boo 是数组 所以 转化为 JSONArray
JSONArray obj = JSONArray.fromObject(boo);
System.out.println(obj);
}
/**
* 字符串-json 转成 JSONObject
*/
@Test
public void test5(){
String json = "{'who':'me','what':[{'way':'fuck you'}],'where':'now'}";
JSONObject obj = JSONObject.fromObject(json);
System.out.println(obj);
System.out.println("************************");
JSONArray arr = obj.getJSONArray("what");
System.out.println(arr);
System.out.println(arr.getJSONObject(0));
System.out.println(arr.getJSONObject(0).getString("way"));
}
}