gpt4 book ai didi

Java json从url获取数据到jcombobox

转载 作者:行者123 更新时间:2023-11-30 06:43:13 25 4
gpt4 key购买 nike

我正在尝试在 netbeans 上开发 swing 应用程序。我正在使用 gson 解码 json。我从 php 站点获取 json 数据。我无法将 json 数据转换为 java 数组。

如何将获取的 json 文件转换为 java 数组。然后我需要设置jcombobox数据源这个数组。

我的 json 文件在 url 上是这样的

[
{
"id": "1",
"ad": "jack",
"latitude": "41.0000",
"longitude": "32.000",
"speed": "",
"hour": "01:28:56",
"day": "18.04.2016",
"adres": "adres",
"resimyol": "http:\/\/maps.google.com\/maps\/api\/staticmap?.jpg"
},
{
"id": "2",
"ad": "Abraham",
"latitude": "41.0000",
"longitude": "41.0000",
"speed": "",
"hour": "01:28:56",
"day": "18.04.2016",
"adres": "adres",
"resimyol": "http:\/\/maps.google.com\/maps\/api\/staticmap?.jpg"
}
]

这是我的 java 文件

public class JsonPojo {
public String id;
public String ad;
public String latitude;
public String longitude;
public String speed;
public String hour;
public String day;
public String adres;
public String resimyol;

public String getId() { return id; }
public String getName() { return ad; }
public String getlat() { return latitude; }
public String getlon() { return longitude; }
public String getspeed() { return speed; }
public String gethour() { return hour; }
public String getday() { return day; }
public String getadres() { return adres; }
public String getresim() { return resimyol; }
}

public static void main(String args[]) {
// got json here...

Gson gson = new Gson();

JsonPojo[] array = gson.fromJson(gson, JsonPojo[].class);
JsonPojo obj = new Gson().fromJson(json, JsonPojo.class);

System.out.println("ID: " +obj.getId());
System.out.println("ID: " +obj.getName());
System.out.println("ID: " +obj.getlat());
System.out.println("ID: " +obj.getlon());
System.out.println("ID: " +obj.getspeed());
System.out.println("ID: " +obj.gethour());
System.out.println("ID: " +obj.getday());
System.out.println("ID: " +obj.getadres());
System.out.println("ID: " +obj.getresim());
}

当我尝试运行项目时,它会给我这样的错误

Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 2 column 13 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at KonumGoster$1.run(KonumGoster.java:145)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 2 column 13 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
... 19 more
BUILD SUCCESSFUL (total time: 5 minutes 5 seconds)

最佳答案

问题是您正在尝试将 gson 对象作为 JSON 传递。而且您也不需要第二次调用来尝试获取单个 JsonPojo,这是行不通的。

Gson gson = new Gson();    

JsonPojo[] array = gson.fromJson(gson, JsonPojo[].class);
JsonPojo obj = new Gson().fromJson(json, JsonPojo.class);

应该是这样的:

Gson gson = new Gson();    

JsonPojo[] array = gson.fromJson(json, JsonPojo[].class);

您将拥有一个对象数组,并且可以迭代它们。

关于Java json从url获取数据到jcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44068233/

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