【问题标题】:JSON parsing to listview/arrayadapterJSON 解析为 listview/arrayadapter
【发布时间】:2018-05-30 10:17:55
【问题描述】:

我正在制作一个将 JSON 数据解析为列表视图的应用程序。这对我不起作用;它没有给我任何数据。

这是我的代码:

public class MainActivity extends AppCompatActivity {

ListView listview;
ArrayAdapter<String> adapter;
String[] data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listview = (ListView)findViewById(R.id.listview);

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

    MPWebservice();

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(MainActivity.this, bestelling.class);
            intent.putExtra("naam", listview.getItemAtPosition(position).toString());
            startActivity(intent);
        }
    });

}


private void MPWebservice() {
    String Webadres = null;
    String dbResult = "empty";
    dbConnect database = new dbConnect(this);

    try {
        String query = "SELECT * FROM orders";
        Webadres = "?query=" + URLEncoder.encode(query, "UTF-8");
        String con = "https://amje.000webhostapp.com/mariosPizzaJSON.php" + Webadres;
        dbResult = database.execute(con).get();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        JSONArray arr = new JSONArray(dbResult);
        JSONObject jo = null;
        data = new String[arr.length()];

        for (int i = 0; i < 1; i++) {
            jo = arr.getJSONObject(i);
            data[i] = jo.getString("name");
        }
        adapter = new ArrayAdapter<String>(this, R.layout.layout_list, R.id.list_item, data);
        listview.setAdapter(adapter);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  }

这是 logcat 中的错误:

of type org.json.JSONObject cannot be converted to JSONArray

这是网络服务中的json:

{
   "orders":[
      {
         "naam":"J. Peeters",
         "adres":"Kettingstraat 12",
         "postcode":"5611 RD",
         "bestelling":[
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"Margarita"
            }
         ]
      },
      {
         "naam":"H. Wissink",
         "adres":"Frederik van Eedenplein 5",
         "postcode":"5611 KT",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Siciliane"
            },
            {
               "Pizza":"4-Stagione"
            }
         ]
      },
      {
         "naam":"M. Huisman",
         "adres":"Hertogstraat 17",
         "postcode":"5611 PB",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"Siciliane"
            },
            {
               "Pizza":"Salami"
            }
         ]
      },
      {
         "naam":"H. Moors",
         "adres":"Mauritsstraat 9",
         "postcode":"5611 GV",
         "bestelling":[
            {
               "Pizza":"Calzone"
            }
         ]
      },
      {
         "naam":"H. Jansen",
         "adres":"Stationsplein 23",
         "postcode":"5611 AC",
         "bestelling":[
            {
               "Pizza":"4-Stagione"
            },
            {
               "Pizza":"4-Stagione"
            }
         ]
      },
      {
         "naam":"G.M. Verkuijlen-vd Ven",
         "adres":"Tramstraat 54",
         "postcode":"5611 CR",
         "bestelling":[
            {
               "Pizza":"Napolitane"
            },
            {
               "Pizza":"Margarita"
            }
         ]
      }
   ]
}

【问题讨论】:

  • 简单使用google GSON ..google吧
  • 同时尝试记录您的请求结果。您的 HTTP 请求可能没有返回 json
  • 嗨@Stefan 首先你必须将你的结果转换为 JSON 对象,因为它是 json 对象并遍历内部订单数组
  • 嘿@Shanmugam 我怎么能用我的代码做到这一点?

标签: java android json parsing


【解决方案1】:

问题出在JSONArray arr = new JSONArray(dbResult); 将其更改为JSONObject arr = new JSONObject(dbResult); 看来您还没有发布完整的 json 数据。如果没有解决,请发布完整的json data

【讨论】:

  • 它不工作。它在 jo = arr.getJSONObject(i) 的 i 上给出错误这是错误消息:JSONObject 中的 .getJSONObject (java.lang.String) 无法应用于 (int)
  • 我还更新了来自 Web 服务的完整 JSON 数据。
  • JSONObject jsonobject = new JSONObject(dbResult); JSONArray jsonarray = jsonobject.getJsonArray("orders");要查找 jsonarray 的长度,请使用; int jsonarrayLength = jsonarray.length(); // 这将计算数组中对象的数量。在这里,您在访问“名称”时采用了错误的方法;不要在这里使用任何循环。
【解决方案2】:

您从 db 获取 JSONObject,而不是数组,该数组位于该对象内部。您应该将来自 Web 服务的响应映射到 JSONObject,并将 bestelling 字段映射到 JSONArray。并且..正如评论中提到的,使用一些库来帮助你,比如 GSON。

【讨论】:

  • 你介意为我做那件事吗?我不知道该怎么做。
  • 如果您没有想法,请提出想法。这很容易,更重要的是,现在你知道该怎么做了。要求某人做某事你知道该怎么做是非常粗鲁的。
  • 他本可以说“不,我不想”,那也很好。不回应也是不礼貌的。另外我很着急,所以我没有时间花 2 个小时在谷歌上试图找到答案。
  • @stefandeboer:milano 是正确的(尽管我可能用更外交的措辞来表达)。我们在这里鼓励自力更生,帮助者与需要帮助的人的比例意味着我们非常警惕屈服于免费工作请求。您能否将获得的新信息用作新研究的起点?做起来可能比你想象的要简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 2012-01-03
  • 1970-01-01
相关资源
最近更新 更多