json 字符串到 java 对象以填充微调器
json string to java object to populate spinner
所以我有一个 Json 字符串,我从 MySQL 服务器
上的 PHP 脚本中检索到
{"Clientid":[24,26,27],"companyname":["chelsea","Microsoft","IBM"]}
并且我一直在尝试将其推入 java 对象中,以便在每行上的微调器显示 "chelsea"、"Microsoft"、[时填充微调器=28=] 但我想在单独的行中显示每个公司,以便用户可以 select 哪个公司
当前代码
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 和 volley 并且弄得一团糟所以我一直在看下面但无法正确地适应 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);
}
我真傻,因为我在开始解析之前就在页面顶部使用了下面的代码,所以在我开始解析之前我花了我的时间重新写了我的sql注意到:(
JSONObject jsonResponse = new JSONObject(response);
所以我有一个 Json 字符串,我从 MySQL 服务器
上的 PHP 脚本中检索到{"Clientid":[24,26,27],"companyname":["chelsea","Microsoft","IBM"]}
并且我一直在尝试将其推入 java 对象中,以便在每行上的微调器显示 "chelsea"、"Microsoft"、[时填充微调器=28=] 但我想在单独的行中显示每个公司,以便用户可以 select 哪个公司
当前代码
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 和 volley 并且弄得一团糟所以我一直在看下面但无法正确地适应 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);
}
我真傻,因为我在开始解析之前就在页面顶部使用了下面的代码,所以在我开始解析之前我花了我的时间重新写了我的sql注意到:(
JSONObject jsonResponse = new JSONObject(response);