比如像这样的JSON:

{

"result":

"{

\"RFM_12_var6\":\"17\",

\"RFM_12_var50\":\"19\",

\"RFM_12_var20\":\"5950.0\",

\"rsk_score\":\"856\",

\"FLAG_6_var4\":\"1\",

\"FLAG_6_var1\":\"3\",

\"dc_flag\":\"debit\",

\"MON_12_var1\":\"12\",

\"crb_score\":\"0.012552760953\",

\"cnp_score\":\"6\",

\"MON_3_var1\":\"3\",

\"RFM_12_var44\":\"6281.0\",

\"RFM_12_var40\":\"0.0\",

}",

"status":"0",

"value":"查询成功"

}

斜杠“\”是为了让后面的引号显示出来,解析式不必在意这个斜杠。这个json result后面的”{…}”这是一个字符串,而不是json对象。以往的话,是这样的:

{

"result":

{

"swift_number":"100032_20160127091434_7510",

"Flag":

{"specialList_c":"1"},

"SpecialList_c":

{

"id":{"bank_lost":"0"},

"cell":{},

"gid":{}

},

"code":"00"

},

"status":"0",

"value":"查询成功"

}

可以看出resullt后面的是 {。。。},而不是“{。。。}” 后面这个例子可以按照json对象处理。

而上面那个需要先把字符串转化为json对象,是这样处理的:

比如要想得到RFM_12_var6 的值

String o = json.getString( "result");

JSONObject object = JSONObject.parseObject(o);

String s = object.getString("RFM_12_var50" );//这样就可以得到RFM_12_var6的值

PS:如何解决json中携带的反斜杠

// 从服务器端获取Json字符串

String data= (JSONObject) event.getData().toString();

// 声明中间变量进行处理

String fly= data.replace("\\", "");

// 处理完成后赋值回去

data= fly.substring(1,fly.length() - 1);

// 这句代码就是为了检验效果的

LogUtils("反转的数据", data);

// 这里用的是Google的GSON

Gson gson = new Gson();

// 这个Json是一个Array类型,即包含多个JSONObject对象

List list = gson.fromJson(data, new TypeToken>() {}.getType());

//接下来你可以做其他你想要做的操作

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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> 8 天前
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> 8 天前
Logo

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

更多推荐