检索JSON数组并将其放入java中的数组中?

检索JSON数组并将其放入java中的数组中?,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我正在检索JSON数据数组并试图显示和访问它。但是当我试图显示它时,它只显示JSON数据数组中的最后一行。当我尝试执行类似于json1.get(2)(json1这里是包含JSONObject的JSONArray)的操作时,我得到了数组越界错误。 请告诉我我做错了什么,我该如何检索JSON 以下是与此问题相关的代码 多谢各位 我正在从Freebies.java类调用UserFunctions.java类的getAllFreebies函数 UserFunctions uf = new UserFun

我正在检索JSON数据数组并试图显示和访问它。但是当我试图显示它时,它只显示JSON数据数组中的最后一行。当我尝试执行类似于json1.get(2)(json1这里是包含JSONObject的JSONArray)的操作时,我得到了数组越界错误。 请告诉我我做错了什么,我该如何检索JSON 以下是与此问题相关的代码

多谢各位

我正在从Freebies.java类调用UserFunctions.java类的getAllFreebies函数

UserFunctions uf = new UserFunctions();
JSONObject json = uf.getAllFreebies();
下面是UserFunctions.java类中我的getAllFreebies()函数的代码:

public JSONObject getAllFreebies(){
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
    JSONObject json  = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
    JSONArray json1 = new JSONArray();
    json1.put(json);
    try {
        System.out.println(json1.get(2));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return json;
}
下面是DB_Functions.php的getFreebies()函数的代码

public function getFreebies(){
$result = mysql_query("SELECT * FROM freebie") or die(mysql_error());
return($result);
}
更新:为了解析JSONArray,我在我的JSONParser类中添加了以下内容 公共JSONArray getJSONArrayFromUrl(字符串url,列表参数){

我更改了getAllFreebie()以检索JSONArray

public JSONArray getAllFreebies() {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", getAllFreebies_tag));
    JSONArray json = jsonParser.getJSONArrayFromUrl(getAllFreebiesURL,
            params);
    return json;
}
publicjsonarray getAllFreebies(){
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“标签”,getAllFreebies_标签));
JSONArray json=jsonParser.getJSONArrayFromUrl(getAllFreebiesURL,
参数);
返回json;
}

我仍然只得到一行而不是所有的行。谁能告诉我我做错了什么。谢谢:(

检查以下代码:

JSONObject json  = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
JSONArray json1 = new JSONArray();
json1.put(json);
try {
    System.out.println(json1.get(2));
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
创建一个新的空
JSONArray
,向其中添加一个元素,然后尝试访问其第二个元素

getJSONFromUrl
应该解析对象或数组吗?我相信您需要它来返回
JSONArray

将您的代码更新为:

编辑:尝试您发布的代码的更新版本:

// Making HTTP request
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse httpResponse = httpClient.execute(httpPost);

    try {
        // Get our response as a String.
        String jsonString = EntityUtils.toString(httpResponse.getEntity());
        Log.d(TAG, "JSON: "+jsonString);

        // Parse the JSON String into a JSONArray object.
        return new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

感谢您的回复。我尝试过这样做,但eclipse给了我一个错误“构造函数JSONArray(JSONObject)未定义”另外,你能看看我的index.php代码并检查是否一切都是正确的吗?我得到了1行,所以我可能没有得到其他行。我读错了你的例子。问题在于
jsonParser.getJSONFromUrl
方法。该方法代码看起来像什么?是你写的还是你正在使用的库?代码如果你的
json
变量是一个包含json数组的字符串,那么我发布的示例就可以了。谢谢你的回复。该函数只需转到URL并检索json。但是我不确定我返回的json数组是包含多行数据还是只有一行数据:实际上你是对的jsonParser是错的。它只解析ObjLemme尝试为JSONArray编写解析器,看看它是否有效。我已经用您刚刚发布的代码的一个经过调整的版本更新了我的答案。请尝试一下,看看它是否对您有效。因此,我检索JSON数组的函数类似于public JSONObject[]getAllFreebies(){}?!@Viking:实际上我会做
JSONArray getAllFreebies()
,难道你不期望JSON freebies数组吗?我也需要在我的JSONParser类中编写一个自定义的getJSONArrayFromURL方法。getJSONFromUrl只是获取JSONObject,而不是JSONArray。
JSONObject json  = jsonParser.getJSONFromUrl(getAllFreebiesURL,params);
JSONArray json1 = new JSONArray();
json1.put(json);
try {
    System.out.println(json1.get(2));
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
// Initialize the array with your JSON string.
JSONArray json1 = new JSONArray(json);

// This line adds your String as the first element to
// the JSON array and is incorrect. You can safely remove it.
//json1.put(json);
// Making HTTP request
try {
    // defaultHttpClient
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    HttpResponse httpResponse = httpClient.execute(httpPost);

    try {
        // Get our response as a String.
        String jsonString = EntityUtils.toString(httpResponse.getEntity());
        Log.d(TAG, "JSON: "+jsonString);

        // Parse the JSON String into a JSONArray object.
        return new JSONArray(jsonString);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;