【问题标题】:Android - Fetching Array with no nameAndroid - 获取没有名称的数组
【发布时间】:2016-02-25 15:17:21
【问题描述】:

我知道我正在获取的已经是一个数组,但我仍然不确定要在这里更改什么。

我有这个可以很好地返回数据,但我不确定如何处理数组以将值放入地图。

protected Void doInBackground(Void... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("http://www.mywebsite.club/api/coffees");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("coffees");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("title", jsonobject.getString("title"));
                map.put("brand", jsonobject.getString("brand"));
                map.put("price", jsonobject.getInt("price"));
                map.put("brandlogo", jsonobject.getString("brandlogo"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

【问题讨论】:

  • 粘贴你要解析的服务器响应
  • 您可以附加 json 数组以响应来自 url 的响应。 "{coffees:" + url_reponse + "}" 之后你可以继续获取 jsonarray 作为咖啡
  • 从您的回复中,我没有找到 coffees 属性作为数组名称,在您的 json 响应中,您只有没有属性名称的数组。因此,您不需要从响应中获取 json 对象,无论您的响应字符串如何,都可以从中获取 json 数组,JSONArray jsonarray = new JSONArray(jsonobject.toString());
  • user370305 请将其作为 aswer 发布,因为它是正确的 :) 并且 cmets 不用于回答
  • @bartol - 我知道,让 OP 有机会尝试一下,如果他能成功,将作为答案发布。谢谢:)

标签: java android android-studio android-json


【解决方案1】:

从您的回复中,我没有找到 coffees 属性作为数组名称,在您的 json 回复中,您只有没有属性名称的数组。因此,您不需要从响应中获取 json 对象,无论您的响应字符串如何,都可以从中获取 json 数组,

protected Void doInBackground(Void... params) {
    // Create an array
    arraylist = new ArrayList<HashMap<String, String>>();
    // Retrieve JSON Array from the given URL address
    jsonarray = new JSONArray(JSONfunctions.getJSONfromURL("http://www.mywebsite.club/api/coffees"));

    try {
         if(jsonarray != null)
         {

          for (int i = 0; i < jsonarray.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            jsonobject = jsonarray.getJSONObject(i);
            // Retrive JSON Objects
            map.put("title", jsonobject.getString("title"));
            map.put("brand", jsonobject.getString("brand"));
            map.put("price", jsonobject.getInt("price"));
            map.put("brandlogo", jsonobject.getString("brandlogo"));
            // Set the JSON Objects into the array
            arraylist.add(map);
          }
       }
    } catch (JSONException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 2018-09-03
    • 2017-12-29
    • 2011-05-05
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多