将jsonarray转换为arraylist

0lvr5msh  于 2021-06-27  发布在  Java
关注(0)|答案(17)|浏览(540)

我正在下载一个json字符串并将其转换为jsonarray。我把它放到一个listview中,以后需要能够从该listview中删除,而且由于jsonarray没有.remove方法(谢谢obama),我正在尝试将它转换为arraylist。
这是我的json(array.tostring()):

[{"thumb_url":"tb-1370913834.jpg","event_id":"15","count":"44","event_tagline":"this is a tagline","event_name":"5th birthday","event_end":"1370919600","event_start":"1370876400"}]

我需要把它放到一个数组中,并且能够通过它们各自的键调用字符串。谢谢你的帮助!

6za6bjd0

6za6bjd01#

java 8样式

JSONArray data = jsonObject.getJSONArray("some-node");

   List<JSONObject> list = StreamSupport.stream(data.spliterator(), false)
                .map(e -> (JSONObject)e)
                .collect(Collectors.toList());
icomxhvb

icomxhvb2#

更简单的java 8替代方案:

JSONArray data = new JSONArray(); //create data from this -> [{"thumb_url":"tb-1370913834.jpg","event_id":...}]

List<JSONObject> list = data.stream().map(o -> (JSONObject) o).collect(Collectors.toList());
rslzwgfq

rslzwgfq3#

试着用这种方法简单地循环,构建你自己的数组。这段代码假设它是一个字符串数组,修改它以适应特定的数组结构应该不难。

JSONArray jsonArray = new JSONArray(jsonArrayString);
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
    list.add( jsonArray.getString(i) );
jljoyd4f

jljoyd4f4#

通用变型

public static <T> List<T> getList(JSONArray jsonArray) throws Exception {

    List<T> list = new ArrayList<>(jsonArray.length());

    for (int i = 0; i < jsonArray.length(); i++) {

        list.add((T)jsonArray.get(i));
    }

    return list;

}

//Usage

List<String> listKeyString = getList(dataJsonObject.getJSONArray("keyString"));
v440hwme

v440hwme5#

ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
 listdata.addAll(jArray);
}

@简化

kq4fsx7k

kq4fsx7k6#

如果您想从json字符串数组中提取数据,下面是我的工作代码。将参数更改为您的数据。

pojo类

public class AllAppModel {
    private String appName;
    private String packageName;
    private int uid;
    private boolean isSelected;
    private boolean isSystemApp;
    private boolean isFav;
}

使用下面的代码行提取数据

try {
    JSONArray jsonArr = new JSONArray("Your json string array");
    List<AllAppModel> lstExtrextData = new ArrayList<>();
    for (int i = 0; i < jsonArr.length(); i++) {
        JSONObject jsonObj = jsonArr.getJSONObject(i);
        AllAppModel data = new AllAppModel();
        data.setAppName(jsonObj.getString("appName"));
        data.setPackageName(jsonObj.getString("packageName"));
        data.setUid(jsonObj.getInt("uid"));
        data.setSelected(jsonObj.getBoolean("isSelected"));
        data.setSystemApp(jsonObj.getBoolean("isSystemApp"));
        data.setFav(jsonObj.getBoolean("isFav"));
        lstExtrextData.add(data);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

它将返回pojo类对象的列表。

yqyhoc1h

yqyhoc1h7#

为了方便起见,可以使用pojo。
像这样试试。。

List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>();

JSONObject jsonObject = new JSONObject(jsonString);
YourPojoObject yourPojo = new YourPojoObject();
yourPojo.setId(jsonObject.getString("idName"));
...
...

yourPojos.add(yourPojo);
deyfvvtc

deyfvvtc8#

JSONArray array = new JSONArray(json);
 List<JSONObject> list = new ArrayList();
 for (int i = 0; i < array.length();list.add(array.getJSONObject(i++)));
q1qsirdb

q1qsirdb9#

public static List<JSONObject> getJSONObjectListFromJSONArray(JSONArray array) 
        throws JSONException {
  ArrayList<JSONObject> jsonObjects = new ArrayList<>();
  for (int i = 0; 
           i < (array != null ? array.length() : 0);           
           jsonObjects.add(array.getJSONObject(i++))
       );
  return jsonObjects;
}
t30tvxxf

t30tvxxf10#

使用gson

List<Student> students = new ArrayList<>();
    JSONArray jsonArray = new JSONArray(stringJsonContainArray);
    for (int i = 0; i < jsonArray.length(); i++) {
        Student student = new Gson().fromJson(jsonArray.get(i).toString(), Student.class);
        students.add(student);
    }
    return students;
ss2ws0br

ss2ws0br11#

只是从主题出发:
将jsonarray转换为list(此处使用jackson jsonarray和object mapper):

ObjectMapper mapper = new ObjectMapper();
JSONArray array = new JSONArray();
array.put("IND");
array.put("CHN");
List<String> list = mapper.readValue(array.toString(), List.class);
myss37ts

myss37ts12#

ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
}
ncgqoxb0

ncgqoxb013#

在java 8中,

IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList())
68bkxrlz

68bkxrlz14#

我有快速解决办法。只是创建一个文件 ArrayUtil.java ```
import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONArray;
import org.json.JSONException;

public class ArrayUtil
{
public static ArrayList convert(JSONArray jArr)
{
ArrayList list = new ArrayList();
try {
for (int i=0, l=jArr.length(); i<l; i++){
list.add(jArr.get(i));
}
} catch (JSONException e) {}

    return list;
}

public static JSONArray convert(Collection<Object> list)
{
    return new JSONArray(list);
}

}

用法:

ArrayList list = ArrayUtil.convert(jArray);

JSONArray jArr = ArrayUtil.convert(list);

lokaqttq

lokaqttq15#

我用的是 Gson (由谷歌提供)。
将以下行添加到模块的 build.gradle :

dependencies {
  // ...
  // Note that `compile` will be deprecated. Use `implementation` instead.
  // See https://stackoverflow.com/a/44409111 for more info
  implementation 'com.google.code.gson:gson:2.8.2'
}
``` `JSON` 字符串:

private String jsonString = "[\n" +
" {\n" +
" "id": "c200",\n" +
" "name": "Ravi Tamada",\n" +
" "email": "ravi@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c201",\n" +
" "name": "Johnny Depp",\n" +
" "email": "johnny_depp@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c202",\n" +
" "name": "Leonardo Dicaprio",\n" +
" "email": "leonardo_dicaprio@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c203",\n" +
" "name": "John Wayne",\n" +
" "email": "john_wayne@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c204",\n" +
" "name": "Angelina Jolie",\n" +
" "email": "angelina_jolie@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "female",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c205",\n" +
" "name": "Dido",\n" +
" "email": "dido@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "female",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c206",\n" +
" "name": "Adele",\n" +
" "email": "adele@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "female",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c207",\n" +
" "name": "Hugh Jackman",\n" +
" "email": "hugh_jackman@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c208",\n" +
" "name": "Will Smith",\n" +
" "email": "will_smith@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c209",\n" +
" "name": "Clint Eastwood",\n" +
" "email": "clint_eastwood@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c2010",\n" +
" "name": "Barack Obama",\n" +
" "email": "barack_obama@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c2011",\n" +
" "name": "Kate Winslet",\n" +
" "email": "kate_winslet@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "female",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" },\n" +
" {\n" +
" "id": "c2012",\n" +
" "name": "Eminem",\n" +
" "email": "eminem@gmail.com",\n" +
" "address": "xx-xx-xxxx,x - street, x - country",\n" +
" "gender" : "male",\n" +
" "phone": {\n" +
" "mobile": "+91 0000000000",\n" +
" "home": "00 000000",\n" +
" "office": "00 000000"\n" +
" }\n" +
" }\n" +
" ]";
``` ContactModel.java :

public class ContactModel {
     public String id;
     public String name;
     public String email;
}

将json字符串转换为 ArrayList<Model> :
注意:您必须导入 java.lang.reflect.Type; :

// Top of file
import java.lang.reflect.Type;

// ...

private void parseJSON() {
    Gson gson = new Gson();
    Type type = new TypeToken<List<ContactModel>>(){}.getType();
    List<ContactModel> contactList = gson.fromJson(jsonString, type);
    for (ContactModel contact : contactList){
        Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
    }
}

希望这对你有帮助。

相关问题