zoukankan      html  css  js  c++  java
  • 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格式的全过程。


  • 相关阅读:
    转帖:解决从9.2.0.1升级到9.2.0.7出现的错误
    最近在公司内部作了一次WCF的培训
    SourceSafe的命令行
    公司再过一两个月就要关门了
    MimeType
    ORACLE 10G 如何使用超过1.7G的内存
    切换网卡
    热键
    Oracle数据库碎片整理
    Hydra安装与使用
  • 原文地址:https://www.cnblogs.com/yeemi/p/7470177.html
Copyright © 2011-2022 走看看