Java 完美解析.plist & 生成plist ,Android 解析.plist

JSON 2018-04-15 23:53:17 18496

今天调试接口,是Apple的接口,格式是  plist  ,有点像  xml  ,但是不是  xml  的协议。开始找了几个网上现成的,测试都有问题。然后找到这个。测试完美解析。


  github  地址:https://github.com/3breadt/dd-plist

Java代码解析.plist

<dependency>
    <groupId>com.googlecode.plist</groupId>
    <artifactId>dd-plist</artifactId>
    <version>1.20</version>
</dependency>

直接摘取我解析的代码。对照看看。

//传入byte[]
NSObject xx = XMLPropertyListParser.parse(bytes);

//你要看你的是什么类型,直接强转
HashMap obj = (HashMap) xx.toJavaObject();

//获取节点
byte[] responseBody = (byte[]) obj.get("ResponseBody");
//GZIP解压
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(responseBody);
try {
    GZIPInputStream ungzip = new GZIPInputStream(in);
    byte[] buffer = new byte[256];
    int n;
    while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
    }
} catch (IOException e) {
    log.error("解压失败",e);
}
//再次解析plist
NSObject body = XMLPropertyListParser.parse(out.toByteArray());
HashMap bodyMap = (HashMap) body.toJavaObject();
result.put("body",new Gson().toJson(bodyMap));

Android 代码解析.plist

您的属性列表文件放入项目文件夹res / raw中,将它们标记为资源文件。然后,您可以InputStream为该资源创建一个资源并将其传递给该资源PropertyListParser。

在这个例子中,你的属性列表文件被称为properties.plist。

try {
  InputStream is = getResources().openRawResource(R.raw.properties);
  NSDictionary rootDict = (NSDictionary)PropertyListParser.parse(is);
  //Continue parsing...
} catch(Exception ex) {
  //Handle exceptions...
}

创建.plist文件

//Creating the root object
NSDictionary root = new NSDictionary();

//Creation of an array of the length 2
NSArray people = new NSArray(2);

//Creation of the first object to be stored in the array
NSDictionary person1 = new NSDictionary();
//The NSDictionary will automatically wrap strings, numbers and dates in the respective NSObject subclasses
person1.put("Name", "Peter"); //This will become a NSString
//Use the Java Calendar class to get a Date object
Calendar cal = Calendar.getInstance();
cal.set(2011, 1, 13, 9, 28);
person1.put("RegistrationDate", cal.getTime()); //This will become a NSDate
person1.put("Age", 23); //This will become a NSNumber
person1.put("Photo", new NSData(new File("peter.jpg")));

//Creation of the second object to be stored in the array
NSDictionary person2 = new NSDictionary();
person2.put("Name", "Lisa");
person2.put("Age", 42);
person2.put("RegistrationDate", new NSDate("2010-09-23T12:32:42Z"));
person2.put("Photo", new NSData(new File("lisa.jpg")));

//Put the objects into the array
people.setValue(0, person1);
people.setValue(1, person2);

//Put the array into the property list
root.put("People", people);

//Save the propery list
PropertyListParser.saveAsXML(root, new File("people.plist"));

支持:

  • 读取/写入文件,流或字节数组的属性列表
  • 在属性列表格式之间转换
  • 属性列表内容作为NeXTSTEP环境中的对象提供(NSDictionary,NSArray,NSString等)
  • 将本机java数据结构序列化为属性列表对象
  • 从属性列表对象反序列化为本机Java数据结构

但是好像toBean的时候不太好使。


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/286.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
json 解析生成工具类 ,JSON操作讲解(附件)
Java 解析JSON,JSON-LIB jar包下载和使用。
Java 解析二维码,google.ZXing 讲解
如何解析JSON数据(详细解答)
阿里云DNS 解析讲解,SEO配置搜索引擎线路解析
使用zxing解析二维码抛出com.google.zxing.NotFoundException 解决方案
Java获取浏览器请求头(User-Agent),分浏览器信息,系统信息的几种办法。
Javascript 生成UUID,Java生成UUID
Redis 单线程模型分
TCP 和 UDP协议详细讲解,优缺点分讲解
最新文章
Elasticsearch 好消息, X-Pack的开源:第一阶段完成 25701
文字转语音接口,支持汉字、英语,文字转音频开放接口,还支持翻译播放 13694
Spring JPA查询,JPA 根据方法名字查询详细介绍 77145
Springboot JPA 执行原生sql ,自定义SQL占位符增加参数 113103
Java 随机从 List 随机获取多个不重复对象,Mysql 随机10条数据 34663
Linux 安装 Redis 详细步骤讲解 13065
Javascript 生成UUID,Java生成UUID 16705
Maven的Mirror和Repository 的详细讲解 61593
JDK1.7中新增自动释放资源接口AutoCloseable讲解 20418
对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景 53306
最热文章
Springboot JPA 执行原生sql ,自定义SQL占位符增加参数 113103
Spring JPA查询,JPA 根据方法名字查询详细介绍 77145
Maven的Mirror和Repository 的详细讲解 61593
对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景 53306
在windows上安装redis并设置密码,IP绑定【提供安装包】 45835
Java 随机从 List 随机获取多个不重复对象,Mysql 随机10条数据 34663
最新VS2012破解 序列号,vs2012旗舰版密钥序列号【收藏】 33827
Elasticsearch操作数据后马上更新的办法 27274
Elasticsearch 好消息, X-Pack的开源:第一阶段完成 25701
JDK1.7中新增自动释放资源接口AutoCloseable讲解 20418
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...