JSON的两种解析方式
来源:互联网 发布:离散最优化算法 编辑:程序博客网 时间:2024/06/07 11:02
1.JSON的定义
JSON:JavaScript Object Notation(JavaScript对象表示法)。
是存储和交换文本信息的语法,类似xml。但是比xml更小、更快、更易解析。
JSON说明:http://www.json.org/json-zh.html
2.JSON语法
数据在 名称/值对中(键值对)
数据由逗号分隔
花括号保存对象
方括号保存数组(集合)
{ "setboxes": [ { "province": "广东", "model": "msd11111", "setboxId": 0, "reserved": null, "vendor": "满洲", "protocolId": 0, "city": "深圳" }, { "province": "广东", "model": "TT10010", "setboxId": 1, "reserved": null, "vendor": "同洲", "protocolId": 1, "city": "深圳" } ], "reserved": null, "version": 0.01, "selectedId": -1}
public class IOUtil {public static String readStreamAsString(InputStream inputStream) throws IOException{BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));String tempData = "";StringBuilder sbBuilder = new StringBuilder();while((tempData=bufferedReader.readLine())!=null){sbBuilder.append(tempData);}inputStream.close();return sbBuilder.toString();}}
//String json 不是文件名,而是由提供文件得到的String。String json = IOUtil.readStreamAsString(new FileInputStream("StbModelMap.json"));JSONObject jo = new JSONObject(String json); //getXXX中的参数直接是JSON文件中对应的名称System.out.println("version:"+jo.getDouble("version"));System.out.println("selectedId:"+jo.getInt("selectedId"));JSONArray ja = jo.getJSONArray("setboxes");for(int i=0;i<ja.elngth();i++){JSONObject jsonObject = (JSONObject )ja.get(i); System.out.println("setboxId:"+ jsonObject .getInt("selectedId"));models.add(jsonObject .getString("model"));provinces.add(jsonObject .getString("province"));}
修改在json信息后再写入json文件:
public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException { String s = IOUtil.readStreamAsString(new FileInputStream( "StbModelMap.json")); JSONObject jsonObject = new JSONObject(s); jsonObject.put("selectedId", 5); FileOutputStream outputStream = new FileOutputStream("StbModelMap.json"); outputStream.write(jsonObject.toString().getBytes()); outputStream.flush(); outputStream.close(); }
注:若models.add(jsonObject .getString("model"));中的"model"字符串不正确,例如写成了"a",则province和model都不能正确解析。
4.JSON的另一种解析(Fastjson)
需要导入jar包--------fastjson-1.1.38.jar
需要自己根据json文件中的信息,创建class,一级一级的创建class,例如上面的信息,要创建两个class,如下:
public class Setboxes { private int setboxId = 0; private String province = null; private String model = null; private String vendor = null; private int protocolId = 0; private String city = null; private String reserved = null;}
public class Info { private String reserved = null; private double version = 0.00; private int selectedId = 0; private Setboxes setboxes = null;}没加getXXX和setXXX。
然后再根据class的信息,来反射,得到class的对象。
public static void main(String[] args) throws FileNotFoundException, IOException { // TODO Auto-generated method stub //从json文件得到json的String String jsonString = IOUtil.readStraemAsString(new FileInputStream("StbModelMap.json")); //解析json Info jsonInfo = JSON.parseObject(jsonString,Info.class); //设置得到的json信息selectedId的值 jsonInfo.setSelectedId(jsonInfo.getSetboxes().getSetboxId()); FileOutputStream fileOutputStream = new FileOutputStream("StbModelMap.json"); //把更改后的json信息再次写入json文件 fileOutputStream.write(JSON.toJSONString(jsonInfo).getBytes()); fileOutputStream.flush(); fileOutputStream.close(); }注:parseObject()相当于:
Info info = new Info;info.setXxx("xxx");info.setXxx("xxx");return info;这就是反射做的事情。
5.Fastjson
Alibaba开源项目,是一个Java语言编写的高性能功能完善的JSON库。
主要方法:
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArraypublic static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObjectpublic static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBeanpublic static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArraypublic static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
0 0
- java解析Json的两种方式
- JSON的两种解析方式
- 解析json串的两种方式
- Json解析两种方式
- Java JSON解析的两种常用方式
- Android中解析JSON的两种方式
- 解析JSON数据后的两种显示方式
- android解析Json常见的两种方式!
- Java解析Json数据的两种方式
- 【代码笔记】iOS-json文件的两种解析方式
- XML解析的两种解析方式
- Ajax的基础请求以及获取JSON的两种解析方式
- JSON与XML的区别比较以及xml两种解析方式的比较
- Ajax的基础请求以及获取JSON的两种解析方式
- ajax重新认识:(2)json数据解析两种方式
- ajax之(2):json数据解析两种方式
- json数组遍历的两种方式
- 使用Json的两种方式
- ZKWeb网站框架介绍
- MySQL 编码设置
- 点出 退出 按钮,防止点击浏览的 后退 按钮仍然可以进系统的解决方案
- Ping程序示例
- android动画,透明度渐变,图片移动
- JSON的两种解析方式
- 2016-01-13(14) 工作笔记
- javascript事件总结
- Eclipse的 debug 介绍与技巧
- eclipse 使用adb命令
- udp发送的流程
- maven 打包插件
- NSNotificationCenter 观察者
- 如何节省开会时间呢?