【问题标题】:json string to java object to populate spinnerjson 字符串到 java 对象以填充微调器
【发布时间】:2015-12-08 07:26:41
【问题描述】:

所以我有一个从 MySQL 服务器上的 PHP 脚本中检索到的 Json 字符串

{"Clientid":[24,26,27],"companyname":["chelsea","Microsoft","IBM"]}

我一直在尝试将其推送到 java 对象中,以便在每行的微调器显示“chelsea”、“Microsoft”、“IBM”时填充微调器,但我想向每家公司展示在单独的行上,以便用户可以选择哪个公司

当前代码

String response = stringBuilder.toString();
Log.d("response", response);
JSONObject jsonResponse = new JSONObject(response);
Log.d("response", jsonResponse.toString());
responseStreamReader.close();
Log.d("length", Integer.toString(jsonResponse.length()));
if (jsonResponse.length() == 0) {
returnedClient = null;
} else {
List<Client> myClients;
myClients = new ArrayList<Client>();
for (int i = 0; i < jsonResponse.length(); i++) {
Client client = new Client();
client.clientid = jsonResponse.getString("Clientid");
client.companyname = jsonResponse.getString("companyname");
myClients.add(client);
}
returnedClient = myClients;
}
} catch (Exception e) {
e.printStackTrace();
}
//Log.d("returnedUser", returnedUser.userpassword);
return returnedClient;

当前的代码显然是错误的代码,所以我一直在查看 Gson 和 Jackson 以及凌空抽射并且一团糟,所以我一直在查看下面的内容,但无法正确匹配 Json 字符串,所以想知道您是否可以提供帮助请或建议更好的解决方案?

JSONArray jarray = jsonResponse.getJSONArray("companyname");
ArrayList<String> xyz= new ArrayList<String>();
for(int i = 0; i < jarray.length(); i++){
JSONArray timeArray =jarray.getJSONObject(i).getJSONArray("companyname");
for(int j = 0; j < timeArray .length(); j++){
xyz.add(timeArray .getString(j));
Log.d("get value",timeArray .getString(j));

解析器是否缺少某些东西,因为无论我更改什么,它似乎都不喜欢我所做的任何事情,它说 json 不能是一个对象,那么它就不能是一个数组,我假设 json 是一个对象,然后是一个数组,但在早上我只是得到错误:(

try {
List<Client> returnedClient = null;
// JSONObject json = new JSONObject(content);
JSONArray json1 = new JSONArray(content);
//JSONObject dataObject1 = json.getJSONObject("Clientid");
//JSONObject dataObject2 = json.getJSONObject("companyname");
//   JSONArray items1 = json.getJSONArray("Clientid");
// JSONObject items2 = json.getJSONObject("companyname");
// JSONArray items1 = json.getJSONArray("Clientid");
JSONArray items2 = json1.getJSONArray(0);
for (int i = 0; i < items2.length(); i++) {
//   JSONArray clientObject1 = items1.getJSONArray(i);
//  JSONObject clientObject2 = items2.getJSONObject(i);
JSONArray clientObject3 = items2.getJSONArray(i);
//  Client client = new Client(clientObject2);
Log.d("response",clientObject3.toString());
// returnedClient.add(client);
}

【问题讨论】:

  • JSONArray timeArray =jarray.getJSONObject(i).getJSONArray("companyname");此行应更改为 String companyValue= jarray.getJSONObject(i).getString();
  • 感谢您的快速响应,但我收到一个错误,即 getJSONObject(i) jsonobject cant ref a int :(

标签: java android json gson android-volley


【解决方案1】:

愚蠢的我,我什至在开始解析之前就在页面顶部使用下面的代码,所以我在树的上方更远了,很遗憾在我注意到之前我需要重新编写我的 sql :(

JSONObject jsonResponse = new JSONObject(response);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多