zoukankan      html  css  js  c++  java
  • java json对象转换

    引入的jar包:

    commons-beanutils-1.9.2.jar

    commons-collections-3.2.1.jar

    commons-lang-2.6.jar

    commons-logging-1.2.jar

    ezmorph-1.0.6.jar

    json-lib-2.4-jdk15.jar

    字符串存json对象里:

    String content = "123";

    String content2 = "456";

    JSONObject jsonObj = new JSONObject();

    jsonObj.put("a",content);

    jsonObj.put("b",content2);

    System.out.println(jsonObj);//输出{"a":"123","b":"456"}

    fwriter = new FileWriter(savefile);

    fwriter.write(jsonObj.toString()); //把jsonObj转成string类型 存到文件中

    fwriter.flush();

    fwriter.close();

    从json对象里获取字符串

    while ((lineTxt = br.readLine()) != null) {

    System.out.println(lineTxt); //从文件中读出的内容

    JSONObject json = JSONObject.fromObject(lineTxt); //转jsonobject

    System.out.println(json.get("I"));//获取I导连

    JSONArray arr = (JSONArray) json.get("I");;//获取I导连 放到集合中

    //List list = (List) json.get("I"); 同上

    System.out.println(list.get(2) instanceof Integer); //true

    }

  • 相关阅读:
    nodejs
    httpClient closeableHttpClient
    re(正则表达式)模块
    ConfigParser模块
    random和string模块
    常用文件操作模块json,pickle、shelve和XML
    time和datetime模块
    os、sys和shutil模块
    内建函数
    生成器、迭代器、装饰器
  • 原文地址:https://www.cnblogs.com/zhi-ming/p/10453158.html
Copyright © 2011-2022 走看看