高兴的鸭蛋 · OSS使用时报错oss2.exception ...· 1 年前 · |
笑点低的松球 · jenkins pipeline ...· 1 年前 · |
还单身的苦瓜 · python ...· 1 年前 · |
可爱的长颈鹿 · ControlNet ...· 1 年前 · |
安静的领带 · 怎么修改进程名_百度知道· 1 年前 · |
我正在下载一个JSON字符串并将其转换为JSONArray。我正在将它放入一个列表视图中,并且需要能够稍后从该列表视图中删除它,而且由于JSONArray没有.remove方法(感谢奥巴马),我正在尝试将其转换为一个数组列表。
下面是我的JSON ( array.toString()):
[
"thumb_url":"tb1370913834.jpg",
"event_id":"15","count":"44",
"event_tagline":"this is a tagline",
"event_name":"5th birthday",
"event_end":"1370919600",
"event_start":"1370876400"
]
我需要将其放入一个数组中,以便能够通过字符串各自的键来调用它们。感谢您的帮助!
您可以只创建一个 JSONObject 本身,而不是将JSON字符串转换为ArrayList,甚至是Map。这个对象可以像你想要的那样使用 get string values by key ,也可以使用 remove objects 。
要从格式正确的JSON字符串创建
JSONObject
,只需调用
appropriate constructor
。
JSONObject json = new JSONObject(jsonString);
我已经使用
Gson
(by Google)
做到了这一点。
将以下行添加到模块的
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);
}
希望这能对你有所帮助。
我有一个快速的解决方案。只需创建一个文件
ArrayUtil.java
import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONArray;
import org.json.JSONException;
public class ArrayUtil
public static ArrayList<Object> convert(JSONArray jArr)
ArrayList<Object> list = new ArrayList<Object>();
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<Object> list = ArrayUtil.convert(jArray);
或
JSONArray jArr = ArrayUtil.convert(list);
泛型变体
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"));
我用来将json数组转换为ArrayList的单行代码,下面列出的json字符串存储在一个名为classData的变量中。
List<String> names = new Gson().fromJson(classData.getAsJsonObject("result").getAsJsonArray("names").toString(), new TypeToken<List<String>>(){}.getType());
{
"result": {
"id": "104297",
"class": "Chemistry",
"total": 57,
"names": [
"Alex",
"Dan",
"Rex",
"Tony",
"Elizabeth"
}
这可以做到(
Java >= 8
):
// Functional (variable)
Function<JSONArray, ArrayList<String>> convert = jsonArray -> (ArrayList<String>) jsonArray.stream()
.map(item -> item.toString())
.collect(Collectors.toList());
// Call
convert.apply(jsonArray);
// Regular method (OOP)
private Function<JSONArray, ArrayList<String>> convert() {
return jsonArray -> (ArrayList<String>) jsonArray.stream()
笑点低的松球 · jenkins pipeline giving inexplicable NullPointerException at GenericDownloadExecutor.java: 52_jenkins_CdCic-CI/CD 1 年前 |
安静的领带 · 怎么修改进程名_百度知道 1 年前 |