【发布时间】:2015-03-09 14:46:07
【问题描述】:
我最近开始开发一个向服务器发出请求并获得 json 响应的应用程序。
“东西”运行良好,直到我不得不在列表中实现新东西,现在我很难修复它。
非常感谢任何帮助:
class RemoteConfig
{
// names and type must match what we get from the remote
String[] username;
ArrayList<accDetails> in_groups;
String[] in_groups_sorted;
class accDetails
{
int group_id;
String group_label;
Boolean _is_system;
}
这只是课程开始的一部分,下面是 json 响应的样子:
{
"username":[
"mike"
],
"in_groups":[
{
"group_id":2,
"group_label":"All users",
"_is_system":true
},
{
"group_id":4372,
"group_label":"Privileged User",
"_is_system":false
},
{
"group_id":4979,
"group_label":"Supervisor",
"_is_system":false
}
]
}
我现在遇到的问题是,如果 _is_system 值为 false,我不知道如何拆分 in_groups 数组列表并进入 String[] in_groups_sorted Group_label 的值。
非常感谢任何帮助。
谢谢你, 迈克
查看回复后,最干净和最简单的是阿贝提供的:
public String[] groupSettings()
{
String[] levels = new String[] {};
if (remoteConfig != null && remoteConfig.in_groups != null){
for (accDetails ad: remoteConfig.in_groups)
{
if (!ad._is_system) {
levels = ArrayUtils.addAll(levels, ad.group_label); ;
}
}
}
return levels;
}
【问题讨论】:
-
你解析json(使用JsonObject/JsonArray等)还是让API为你构建Object?
-
感谢大家的帮助。我的想法太远了,我忘了保持简单。我使用了@Abbe Resina 提供的部分代码,它工作正常。其他的也成功了,但这是最简单的
标签: java android json arraylist