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
- Json 解析 案例
- Json解析小案例
- Json解析案例
- json数据解析案例
- 简单JSON解析案例出错
- java 解析json小案例
- unity解析Json字符串案例
- Json获取、解析案例演示----xml解析
- Android 使用Gson解析json案例详解
- 使用Gson解析Json数据案例
- Android 使用Gson解析json案例详解
- Android 使用Gson解析json案例详解
- fastJson解析多层Json数据案例
- json使用案例以及几种解析json方式
- JSON使用案例--一次性解析比较大的json文件
- Android开发之json解析案例详解==++
- AsyncHttp获取网络Json并解析小案例
- Android开发之json解析案例详解==++
- nodeValue以及其与value的区别以及JS nodeName、nodeValue、nodeType返回类型
- 阶乘因式分解(一)
- iOS每日一点-使用多张连续的图片做小动画
- c之求转置矩阵--南阳oj-29
- hdu 2457 DNA repair(AC自动机+DP)
- Json 解析 案例
- 数据库简单工具封装,查询,更新,分页的要单独写一个查询,多多指教
- matlab创建三维数组的三种方法
- Android之MIME TYPE
- Linux中的shell脚本浅析
- html 标签用法
- 6174问题(书上P77)
- matlab gif文件生成
- 将.c文件进行封装