我有一个变量,它是一串 JSON,这个 JSON 是一个对象数组:
String actions = "[{'title': 'BBC', 'url': 'https://www.bbc.co.uk'}, {'title': 'GOOGLE', 'url': 'https://www.google.com'}]"
我需要将它转换成我可以在我的 android 应用程序中使用的东西,所以我可以迭代一个数组或对象。我该怎么办?
使用 Gson
String actions = "[{'title': 'BBC', 'url': 'https://www.bbc.co.uk'}, {'title': 'GOOGLE', 'url': 'https://www.google.com'}]";
List list = new Gson().fromJson(actions, List.class);
如果您的 JSON 具有已知结构,您可以将其映射到类,例如:
class Website {
String title;
String url;
}
Type type = new TypeToken<List<Website>>() {}.getType();
List<Website> list = new Gson().fromJson(actions, type);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句