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

    }

  • 相关阅读:
    hdu3487 Play with Chain
    poj3481
    [HNOI2002]营业额统计
    poj3468 A Simple Problem with Integers
    [NOI2004]郁闷的出纳员
    UVa1308 LA2572
    20130620
    poj3580
    20130618
    C++类模版学习笔记
  • 原文地址:https://www.cnblogs.com/zhi-ming/p/10453158.html
Copyright © 2011-2022 走看看