gpt4 book ai didi

java - JSON 字符串到 Object[]

转载 作者:行者123 更新时间:2023-12-01 19:49:18 24 4
gpt4 key购买 nike

我需要将 JSON 字符串转换为 Object[]。

我尝试过 link1link2并没有帮助我。

编写如何获取 JSON 字符串的代码:

public static String getListJsonString() {
String getListsUrl = BASE_URL + "lists";
String result = "";
try {
URL url = new URL(getListsUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + getAuthStringEnc());
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);

int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}

return result;
}

这是我的 JSON 示例:

enter image description here

之后我必须以这种方式填充 ChomboBox(这是示例):

Object[] lists = getLists();
for(Object list : lists){
System.out.println("fill combobox");
}

最佳答案

您可以使用Gson、TypeToken和JSONObject,例如:

final static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
final Type objectType = new TypeToken<Object>(){}.getType();

JSONObject obj = new JSONObject(result);

Vector<Object> lists = gson.fromJson(obj.toString(), objectType);

关于java - JSON 字符串到 Object[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52000580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com