所以我有一个Json字符串,我从MySQL服务器上的PHP脚本中检索到
{"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是一个对象然后是数组,但在mormnt我只是得到错误:(
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);
}
答案 0 :(得分:0)
JSONObject jsonResponse = new JSONObject(response);