当前位置:   article > 正文

java类中写json字符串,如何将JSON字符串放入Java列表中

java把json格式字符串的key和val放进列表

I'm getting a JSON array in string like

[ { "id":"ca.Primary_Diagnosis_Dt",

"field":"ca.Primary_Diagnosis_Dt",

"type":"date",

"input":"text",

"operator":"not_equal",

"value":"2016/06/07"

},

{ "id":"ca.Clinical_Stage",

"field":"ca.Clinical_Stage",

"type":"integer",

"input":"select",

"operator":"equal",

"value":"I"

}

]

i just want to save the value of id ,operator and value in LIST please help

解决方案

First create a class to store your values :

class Data{

String id;

String operator;

String value;

}

Then iterate over the json :

JSONArray jsonArr = new JSONArray("[JSON Stirng]");

List dataList = new ArrayList<>();

for (int i = 0; i < jsonArr.length(); i++) {

JSONObject jsonObj = jsonArr.getJSONObject(i);

Data data = new Data();

data.id = jsonObj.getString("id");

data.operator = jsonObj.getString("operator");

data.value = jsonObj.getString("value");

dataList.add(data);

}

Now dataList has your data!

P.S. : Use getter/setters in Data class

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/618603
推荐阅读
相关标签
  

闽ICP备14008679号