josn数据格式如下,我需要获取text的值并保存到实体类的对应字段并入库。
先将数组变为对象,然后将josnobject转为string,在调用getString()获取值
在这里插入图片描述
第一步:取出value中的值,这个时候还是数组结构
在这里插入图片描述
第二步:调用JSONArray.parseArray(),将json格式的数据转换成数组格式,这样就可以使用数组相关的方法。
在这里插入图片描述
我们调用get方法,这时就变成了object类型的数据,再将object类型的数据转换为string类型,即调用tostring方法
在这里插入图片描述

在这里插入图片描述
第三步:这时就变成了json格式的数据
在这里插入图片描述
第四步:使用JSONObject.parseObject方法,是将string转化为相应的JSONObject对象,其中string是“键值对”形式的json字符串,转化为JSONObject对象之后就可以使用其内置的方法,如调用getString方法
在这里插入图片描述

在这里插入图片描述
第五步:在调用getString方法,里面传入的是key,如取出text的值
在这里插入图片描述
在这里插入图片描述
最终将这个解析的结果保存到实体类对应的字段中
在这里插入图片描述
或者遍历也可以,具体实现如下:

                    String lableValue = formLable.getValue();
                    JSONArray jsonArray = new JSONArray(lableValue);
                    for (int i=0;i<jsonArray.size();i++){
  //这里是想取出来text的值,而且我知道text的下边是2,但是不能写死,所以取值的时候直接jsonArray.size()-1 ,这样取值。也可以jsonArray.get(i),这样的话也能保证最后一个下标代表的key就是text,再调用toString方法获取值
                       formBusinessTravelApply.setDeptName(jsonArray.getJSONObject(jsonArray.size()-1).get("text").toString());
                    }

描述有误的地方欢迎大家指正,大家有问题可加qq 876942434。一起进步~

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> 3 天前
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> 4 天前
Logo

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

更多推荐