如何使用Gson解析包含数组的JSON文件

时间:2018-05-18 16:11:48

标签: android json kotlin ion

我正在尝试使用this,Android版库和Gson从ion网址读取JSON文件。

处于当前状态的JSON文件:

{
    "Excited":["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
                "https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
                "https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"],

    "Sad":["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
            "https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"]

}

重要提示:虽然现在JSON文件有两个数组,“Excited”和“Sad”,但未来可能会有更多数组。但是,文件的基本结构将始终是一系列JSON数组。

我的目标是将包含两个(但可能更多)数组的JSON对象转换为列表列表。

到目前为止,这是我的代码,它解析了URL并返回一个名为“result”的Gson JsonObject

Ion.with(applicationContext)
                .load("https://raw.githubusercontent.com/vedantroy/image-test/master/index.json")
                .asJsonObject()
                .setCallback { e, result ->
                    //Do something...
                }

此代码也可以写成:

Ion.with(applicationContext)
                .load("https://raw.githubusercontent.com/vedantroy/image-test/master/index.json")
                .asJsonObject()
                .setCallback(object : FutureCallback<JsonObject> {
                    override fun onCompleted(e: Exception?, result: JsonObject?) {
                        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                    }

                })

2 个答案:

答案 0 :(得分:3)

循环遍历<a className="footer__links--contact--phone" href={`tel:${JSON.stringify(messages.phone)}`}> JsonObject并将每个entrySet添加到列表中,我使用我的Java代码,如果需要,您可以将其转换为Kotlin:

JsonArray

答案 1 :(得分:0)

如果你可以重构这个JSON,我会使用这样的东西,对我来说这更容易序列化/反序列化并且看起来更加语义(并且是一个JSON数组,你发送的JSON只是一个JSON对象)

[{
    "mood": "Excited",
    "gifs": ["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
        "https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
        "https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"
    ]
},

{
    "mood": "Sad",
    "gifs": ["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
        "https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
        "https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
        "https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"
    ]
}
]

使用这个JSON,您可以在Java中反序列化,因此您必须使用Ion库来适应Kotlin

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

public class Deserializer {

    public static void main(String[] args) {
       JSONArray json = new JSONArray(
            "[{\"mood\": \"Excited\",\"gifs\": [\"https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif\",\"https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif\",\"https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif\"]},{\"mood\": \"Sad\",\"gifs\": [\"https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif\",\"https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif\",\"https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif\",\"https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif\"]}]");
       Gson gson = new Gson();
       List<YourObject> test = gson.fromJson(json.toString(), new TypeToken<ArrayList<YourObject>>() {}.getType());
       test.forEach(System.out::println);
    }

    public static class YourObject {

        private String mood;
        private List<String> gifs;

        @Override
        public String toString() {
            return this.mood.toString() + " " + this.gifs.toString();
       }
    }
}