android - 如何在Android中设计javaBean
问题描述
我得到像这样的json:
{
"success":[
{"key":"headache", "value":false},
{"key":"fatigue", "value":false},
{"key":"sputum,color", "value": [
"none",
"green",
"yellow",
"white",
"rustColor",
"grayBlack"]
}
],
"errorCode":0}
我尝试解析 json ,因为我需要将键和值放入 RecyclerView。
JSONObject object = new JSONObject(result.toString());
int errorCode = object.getInt("errorCode");
if (errorCode == 0){
JSONArray array = object.getJSONArray("success");
for (int i = 0; i < array.length(); i++){
JSONObject newObject = array.getJSONObject(i);
String keyStr = newObject.getString("key");
Object value = newObject.get("value");
if (value instanceof String){
String valueString = value.toString();
else if (value instanceof Boolean)
Boolean valueBoolean = (Boolean) value;
}
}
有没有人可以帮助我?
解决方案
最好的方法是修复您的 JSON 问题,因为对于不同的值类型使用相同的键是不正确的。
如果你不能,我想你会这样做:
try {
boolean boolValue = newObject.getBoolean("value");
// here, you will have a Boolean
} catch (JSONException e) {
// if an Exception is thrown, you have to deal with an array
JSONArray array = newObject.getJSONArray("value");
// here, you will have an Array
}
推荐阅读
- javascript - 如何返回 API 的响应,它显示未定义,但是当我尝试打印它时,它准确地显示了我想要的
- javascript - 我想通过单击按钮停止传播...在此代码中,我可以通过复选框执行操作,但我想通过单击按钮执行相同的操作
- javascript - 如何在java脚本中合并两个数组值
- python - 如何将纯文本转换为可解析文本(每个单词之间有百分比符号)?
- python - 如何用一个函数替换字符串数据框中的多个字符
- firebase - 如何查看firestore中快照之间的变化?| 扑
- google-cloud-platform - Cloud Shell 技术错误 - 有没有其他人经历过这个?
- php - 使用 php从标签中获取 URL
- c - C中#define的逆向
- react-native - 如何在 React Native 中创建具有复杂形状的底部标签栏