我有一个像这样回来的json数组
[{"item":"value"},{"item":"value"},{"item":"value"},{"item":"value"}]
我试图遍历这个数组并使用下面的代码获取“item”,但是这个值永远不会被抓取,我可以确认该数组确实有值,但不知怎的,这总是返回null
ArrayList<ArrayList<Object>> mainList = new ArrayList<ArrayList<Object>>();
String json = getJSON(url, null);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(json);
int counter = 0;
JSONArray itemArray = new JSONArray();
itemArray.put(jsonObject);
while(counter < itemArray.length()){
//create an inner array
ArrayList<Object> innerList = new ArrayList<Object>();
//grab the contents of the post
JSONObject item = itemArray.getJSONObject(counter);
//place items into inner array
innerList.add(counter + 1);
innerList.add(item.getString("item"));
//place inner array into main array
mainList.add(innerList);
counter++;
}
return mainList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
答案 0 :(得分:1)
关于你的问题的信息很少(stacktrace ??),但这似乎不正确
item.getString("name");
尝试
item.getString("item");
答案 1 :(得分:0)
以错误的方式创建数组:
window.fill((255, 255, 255), Rect(100, 100, 100, 100)) # outer rect
window.fill((0, 0, 0), Rect(110, 110, 80, 80)) # inner rect
答案 2 :(得分:0)
我用这个
JSONParser parser = new JSONParser();
boolean downloaded = parser.downloadJSON(url, JSONParser.HTTP_GET);
if (!downloaded) throw new Exception("Oops");
JSONObject root = parser.getRoot();
JSONArray list = parser.getList(root, "list");
if (list != null)
{
for (int i = 0; i < list.length(); i++)
{
JSONObject jo = list.getJSONObject(i);
if (jo == null) continue;
// let's go...
}
}
和这......
public class JSONParser
{
public static int HTTP_GET = 0;
public static int HTTP_POST = 1;
private String json;
// constructor
public JSONParser()
{
json = null;
}
/**
* Scarica il documento JSON e lo rende disponibile ai vari metodi sempre
* disponibili in questa classe
*
* @param url indirizzo url da cui scaricare il documento JSON
* @param type tipo di richiesta GET o HTTP
* @return indica se è stato scaricato, altrimenti lancia eccezione
*/
public boolean downloadJSON(String url, int type) throws Exception
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpUriRequest httpRequest = type == HTTP_POST ? new HttpPost(url) : new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();
json = EntityUtils.toString(httpEntity);
return true;
}
catch (UnsupportedEncodingException ex)
{
WLogger.error("XMLParser", "UnsupportedEncodingException downloadJSON", ex);
throw ex;
}
catch (ClientProtocolException ex)
{
WLogger.error("XMLParser", "ClientProtocolException downloadJSON", ex);
throw ex;
}
catch (IOException ex)
{
WLogger.error("XMLParser", "IOException downloadJSON", ex);
throw ex;
}
// WHttpRequestAsyncTask task = new WHttpRequestAsyncTask();
// task.setType(type);
// task.execute(url);
// json = task.getResult();
// return json != null;
}
public void setJsonData(String json)
{
this.json = json;
}
public JSONObject getRoot()
{
try
{
return new JSONObject(json);
}
catch (JSONException ex)
{
WLogger.error("JSONParser", "getRoot error", ex);
return null;
}
}
public JSONArray getList(JSONObject object, String name)
{
try
{
if (object == null || Utility.isNullOrEmpty(name) || object.isNull(name))
return null;
return object.getJSONArray(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getList error", ex);
return null;
}
}
public JSONObject getObject(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getJSONObject(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public String getString(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getString(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Integer getInt(JSONObject object, String name)
{
try
{
if (object == null || object.isNull("id")) return null;
return object.getInt("id");
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Double getDouble(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getDouble(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Long getLong(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getLong(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
public Boolean getBoolean(JSONObject object, String name)
{
try
{
if (object == null || object.isNull(name)) return null;
return object.getBoolean(name);
}
catch (Exception ex)
{
WLogger.error("JSONParser", "getObject error", ex);
return null;
}
}
}
答案 3 :(得分:0)
创建相应的Class并使用Gson库解析json字符串并创建对象。如此整洁干净,无混乱
Gson gson = new Gson();
MyClass object = gson.fromJson(jsonString,MyClass.class);