简体   繁体   English

Android / Java:解析来自Rails的JSON数组

[英]Android / Java : Parse Array of JSON coming from Rails

My server is written in Rails 4.x and to the to_json method I'm using to return records back through the API is delivering an array of JSON to Android that looks like this: 我的服务器是用Rails 4.x编写的,并且是通过API返回记录的to_json方法,用于向Android提供如下所示的JSON数组:

[{"id":503240,"name":"bob"},{"id":503241,"name":"Tom"}]

It comes down to Android fine and I have no problems using it as a string and logging it. 它归结为Android很好,使用它作为字符串并记录它没有问题。

Now I want to loop through each item of the array grab the id (ultimately to show it on a ListView). 现在,我想遍历数组的每个项目并获取id(最终将其显示在ListView上)。

The first thing I need to do is "see" each individual object. 我要做的第一件事是“看到”每个单独的对象。 So I am parsing the above using the following line of code: 因此,我使用以下代码行解析以上内容:

JSONArray jsonResponse = new JSONArray(jsonString);

It "parses" with no errors, but ... when I try to do something like jsonResponse[1], or jsonResponse.getObject(1) to get my hands on the one of the elements of the array (just to log it for example), I can't get past the IDE telling me I'm doing it wrong. 它“解析”没有错误,但是...当我尝试执行jsonResponse [1]或jsonResponse.getObject(1)之类的操作时,便可以使用该数组的其中一个元素(只是将其记录为例如),我无法通过IDE告诉我做错了。

If I can address and manipulate the individual json objects, I'm sure I'll have no problems addressing the individual data elements. 如果我可以解决和处理单个json对象,那么我确定解决单个数据元素不会有问题。 It's the arrayness of this that is causing problems. 正是这种复杂性导致了问题。

I'm a little new to Java and I'm spoiled by Ruby and Swift. 我对Java有点陌生,但对Ruby和Swift却很满意。 I'm missing something basic. 我缺少基本的东西。 What am I doing wrong? 我究竟做错了什么?

Thanks in advance. 提前致谢。

There seems to be no method getObject on JSONArray instances in Android. Android中的JSONArray实例上似乎没有方法getObject Documentation here . 文档在这里

I guess what you need is: 我想您需要的是:

jsonResponse.getJSONObject(1)

because jsonResponse is an Array of JSONObject s 因为jsonResponseJSONObject的数组

JSONArray contains a method called JSONObject getJSONObject(int position) , which returns the item of type JSONObject which is found in the JSONArray at the position you asked for. JSONArray包含一个名为JSONObject getJSONObject(int position) ,该方法返回JSONObject类型的项目,该项目在JSONArray中您需要的位置找到。

Now if you want to populate the json array into a listview, you should create an adapter for the listview and each row will contain the data of each item from the array. 现在,如果要将JSON数组填充到列表视图中,则应为该列表视图创建一个适配器,并且每一行将包含该数组中每个项目的数据。

The most common adapter is extending the BaseAdapter class, you will notice that you will need to implement some methods, the main are 最常见的适配器是扩展BaseAdapter类,您会注意到您将需要实现一些方法,主要是

  • getCount - which usually returns the number of items you have in the array, so basically when you return 0 , you will see nothing, but if you return 1 or the size of the array - the listview will have the same number of rows. getCount通常返回数组中的项数,因此基本上,当您返回0时,您将看不到任何内容,但是如果返回1或数组的大小,则listview将具有相同的行数。
  • getView - which returns the view of each row, in this method you will need to inflate the view of the row (you will need to create an xml file that will be the layout of the row), after you inflate the row and all of the elemnts like textview, imageview , you will need to populate the data from each item in your array. getView返回每行的视图,在此方法中,您需要在对行和所有行进行膨胀之后,使行的视图膨胀(您将需要创建一个将作为行布局的xml文件)。像textview和imageview这样的元素,您将需要填充数组中每个项目的数据。 the mechanism works in this way: one of the arguments of this method is the position of the row, so when the user is scrolling the list, this method will be called and the position will be updated automatically, in that case to get the specific item from your array you just need to call yourJSONArray.getJSONObject(position) , and then fill each text field with the data you want. 该机制以这种方式工作:此方法的参数之一是行的位置,因此,当用户滚动列表时,将调用此方法并且位置将自动更新,在这种情况下,可以获取特定的数组中的项目,只需调用yourJSONArray.getJSONObject(position) ,然后用所需的数据填充每个文本字段。

Notice that in order to create some usefull performence there is something called ViewHolder, you should create this one in order to avoid from android to create & inflate the row-view each time when the user is scrolling. 请注意,为了创建一些有用的性能,有一种称为ViewHolder的东西,您应该创建一个ViewHolder,以避免在每次用户滚动时从android创建并填充行视图。

One more thing is that you can also read and check about recycleview. 还有一件事是,您还可以阅读并检查recycleview。

To loop through a JSONArray just use a for loop like this 要遍历一个JSONArray,只需使用这样的for循环

try {
    JSONArray array = new JSONArray("[{\"id\":503240,\"name\":\"bob\"},{\"id\":503241,\"name\":\"Tom\"}]");

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);

        Log.d("TAG", "Object #" + i + " id: " + object.getInt("id") + " name: " + object.getString("name"));
    }

} catch (JSONException e) {
    Log.e("TAG", "Cannot parse json :", e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM