json数据转化格式

http://blog.csdn.net/lovehongyun/article/details/2971341

将文件存储成json格式,在数据流读文件,将内容装换成字符串,再讲字符串转换成json格式,可轻松获得自己想要的数据。

eg:在包中创建一个eg.file文件里面存储json数据。

{
  "name":"name",
  "old":"old",
  "color": [
    {
      "yello":"ok",
      "red":"no",
    }
  ]
}


}

//获得json数据的文件位置
String path=Test.class.getResource("eg.file").getFile();
//读文件;
InputStream is=null;
OutputStream  op=null;
StringBuilder sb=new StringBuilder();
try{
byte[] b=new byte[1024];
is=new FileInputStream(path);
op=new FileOutputStream("c:\\test.txt");

int size =-1;
while((size=is.read(b))!=-1){
sb.append(new String(b,0,size));
op.write(b);
op.flush();
}
}catch(IOException e){
System.out.println(e+"error");
}finally{
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}

JSONObject jsonObject = JSONObject.parseObject(jsonString);
service = jsonObject.getString("name");
serviceIcon = jsonObject.getString("old");


JSONArray confArray = JSONArray.parseArray(jsonObject.getString("color"));
configuration = new ArrayList<PackageConfiguration>();
PackageConfiguration conf = null;
for (Object confString : confArray.toArray()) {
conf = new PackageConfiguration((JSONObject) confString);
configuration.add(conf);
}
confArray.clear();
jsonObject.clear();
}

即完成了读取json文件,并将数据存储成json格式的全过程。


posted @ 2015-10-15 17:05  yeemi  阅读(186)  评论(0编辑  收藏  举报