所以我有一个从我的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);
}
哦,愚蠢的我是在树的上方,因为我在开始解析之前就使用了页面顶部下面的代码,可惜的是,在我注意到之前,我不得不重新编写sql了:(
JSONObject jsonResponse = new JSONObject(response);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句