JSON文字列をダウンロードしてJSONArrayに変換しています。それをリストビューに入れて、後でそのリストビューから削除できるようにする必要があります。JSONArrayには.removeメソッドがないため(Obamaに感謝)、配列リストに変換しようとしています。
ここに私の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"}]
それを配列に入れて、それぞれのキーで文字列を呼び出すことができる必要があります。ヘルプを感謝します!
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));
}
}
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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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\": \"[email protected]\",\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);
この方法を試してみてくださいループを繰り返して、独自の配列を作成しますこのコードは、文字列の配列であると想定しています。特定の配列構造に合わせて変更することは難しくありません。
JSONArray jsonArray = new JSONArray(jsonArrayString);
List<String> list = new ArrayList<String>();
for (int i=0; i<jsonArray.length(); i++) {
list.add( jsonArray.getString(i) );
JSON文字列をArrayListまたはMapに変換する代わりに、 JSONObject 自体を作成できます。このオブジェクトには、必要に応じて キーで文字列値を取得 と、 オブジェクトを削除 の機能があります。
適切にフォーマットされたJSON文字列からJSONObject
を作成するには、単に 適切なコンストラクター を呼び出します。
JSONObject json = new JSONObject(jsonString);
JSONArray array = new JSONArray(json);
List<JSONObject> list = new ArrayList();
for (int i = 0; i < array.length();list.add(array.getJSONObject(i++)));
Java 8では、
IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList())
便利にするには、POJOを使用してください。
このようにしてみてください。
List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>();
JSONObject jsonObject = new JSONObject(jsonString);
YourPojoObject yourPojo = new YourPojoObject();
yourPojo.setId(jsonObject.getString("idName"));
...
...
yourPojos.add(yourPojo);
迅速な解決策があります。ファイルを作成してくださいArrayUtil.Java
ObjectMapper mapper = new ObjectMapper();
List<Student> list = Arrays.asList(mapper.readValue(jsonString, Student[].class));
使用法:
ArrayList<Object> list = ArrayUtil.convert(jArray);
または
JSONArray jArr = ArrayUtil.convert(list);
汎用バリアント
private interface ParseCallable<T> {
T call(JSONArray jsonArray, int index) throws JSONException;
}
private <T> List<T> getList(JSONArray jsonArray, ParseCallable<T> callable) throws Exception {
List<T> list = new ArrayList<>(jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
T t = callable.call(jsonArray, i);
list.add(t);
}
return list;
}
使用法
List<String> listKeyString = getList(dataJsonObject.getJSONArray("keyString"), new ParseCallable<String>() {
@Override
public String call(JSONArray jsonArray, int index) throws JSONException {
return jsonArray.getString(index);
}
});
List<Integer> listKeyInteger = getList(dataJsonObject.getJSONArray("keyInteger"), new ParseCallable<Integer>() {
@Override
public Integer call(JSONArray jsonArray, int index) throws JSONException {
return jsonArray.getInt(index);
}
});
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;
}
ArrayList<String> dataList = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonObject;
for(Object obj : jsonArray){
dataList.add((String)obj);
}