一、思路:

之前讲过一次JSON解析,但是当时还是懵懵的,现在再次复习一下:
如果你拿到的是一个String类型的字符串,但是,它的外形是Json格式的,取出其中的键值对(尤其是值)?
这里的思路是,先把String类型的字符串,转换成Json类型,然后再取就容易多了;但是是通过截取字符串来去,还是怎么弄?这里,有一个现成的包:

import com.alibaba.fastjson.JSON;

这个是alibaba的解析JSON的一个包,里面有个parse()方法,专门用来解析String类型,但是具有Json格式的字符串s1;

{
    "SHEETSTATUSNAME": null,
    "WCSJ": "",
    "LASTOPENTIME": "2017-08-18 10:09:19",
    "DATASTATUSID": "1",
    "FLOWNODEID": "2",
    "YSR": null,
    "OPENERID": null,
    "SHOWORDER": "144",
    "SHEETNAME": null,
    "ZGSJ": "2017-08-31",
    "OPENER": null,
    "DATATYPE": null,
    "CREATEDATE": "2017-08-16 11:16:26",
    "WCZT": null,
    "ZRR": null,
    "FXSJ": "2017-08-21",
    "CREATEORGID": "92",
    "WCQK": null,
}

这里只是具有JSON形式的,String类型的字符串,不是Json对象(JSONObject);

解析,核心方法是com.alibaba.fastjson.JSON中的parse()方法;

com.alibaba.fastjson.JSONObject jsonObject= (com.alibaba.fastjson.JSONObject)  com.alibaba.fastjson.JSON.parse(s1);

这里,因为没有上下文,包名都写全了,但是不方便看,如果简写可以这样(仅仅是为了方便观看,上面的是标准写法):

  JSONObject jsonObject= (JSONObject) JSON.parse(s1);
  //这里的s1就是那个Json形式的字符串的名字;

二、解析

之后就是重复性劳动:

         List<CheckList> checkLists = new ArrayList<>();
           CheckList ck = new CheckList();
           for(String s1 :checkListStr){
               JSONObject jsonObject= (JSONObject) JSON.parse(s1);
               //System.out.println(jsonObject);
               ck.setROW_INDEX((String)jsonObject.get("ROW_INDEX"));
               ck.setFXR((String) jsonObject.get("FXR"));
               ck.setPATTERNID((String) jsonObject.get("PATTERNID"));
               ck.setZGCS((String) jsonObject.get("ZGCS"));
               ck.setSHEETID((String) jsonObject.get("SHEETID"));
               ck.setCREATEUSERID((String) jsonObject.get("CREATEUSERID"));
               ck.setYSR((String) jsonObject.get("YSR"));
               ck.setFLOWNODENAME((String) jsonObject.get("FLOWNODENAME"));
               ck.setFLOWNODEID((String) jsonObject.get("FLOWNODEID"));
               //System.out.println(ck.getYSR());
               //System.out.println(s1);
               checkLists.add(ck);
           }
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 16 小时前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 23 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐