Json 解析 案例

来源:互联网 发布:前锦网络信息技术公司 编辑:程序博客网 时间:2024/06/05 07:03

 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对象 转换 */@Testpublic void test1(){//创建  Studnet 对象 -> s1Student s1 = new Student();s1.setAge(22);s1.setName("fuck");s1.setScore(91.0);//将 s1 对象 放入 listList<Student> list = new ArrayList<Student>();list.add(s1);System.out.println("************************************");//将 list转为 JSONArrayJSONArray array = JSONArray.fromObject(list);System.out.println(array.toString());System.out.println("************************************");//将 s1 转为 JSONArrayJSONArray array2 = JSONArray.fromObject(s1);System.out.println(array2.toString());System.out.println("************************************");//将 s1 转为 JSONObjectJSONObject 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 转为 listList<Student> list2 = JSONArray.toList(array2);System.out.println(list2);}/** * 创建一个 JSONObject  * 并 得到 json 里的数据 */@Testpublic 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  */@Testpublic 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 里 还有一个 jsonObjectJSONArray arr = obj.getJSONArray("go");System.out.println("arr: "+arr);System.out.println(arr.getJSONObject(0));}/** * 一般数组 转化为 json */@Testpublic void test4(){boolean[] boo = new boolean[]{false,true,false,true};// boo 是数组 所以 转化为 JSONArrayJSONArray obj = JSONArray.fromObject(boo);System.out.println(obj);}/** * 字符串-json  转成 JSONObject */@Testpublic 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"));}}



0 0
原创粉丝点击