使用 Gson 读取 JSON 字符串

发布于 2024-10-04 08:04:35 字数 342 浏览 2 评论 0 原文

我一直在谷歌上搜索试图解决这个问题,但我似乎做不到。我有以下 json 字符串,该字符串从我需要与之交互的另一个源返回到我的 java 小程序。

{
 "A01": {"Status": "Ready", "Time": "00:00"}, 
 "A02": {"Status": "Ready", "Time": "00:00"}, 
 ......
}

目前我不确定应该如何使用 Gson 将其解析到我的小程序中。当我与该程序的设计者交谈时。 json 字符串设计用于 php 而不是 java,因此当我在 php 中对其进行解码时,它给了我一个很好的多维关联数组。

对此有何建议。

I've been pursing around Google trying to figure this out, but I can't seem to do it. I have the following json string that is returned to my java applet from another source that i need to interact with.

{
 "A01": {"Status": "Ready", "Time": "00:00"}, 
 "A02": {"Status": "Ready", "Time": "00:00"}, 
 ......
}

At the moment I'm not sure how i should use Gson to parse that into my applet. When i talked to the designers of that program. The json string was designed for use in php not java so when i decoded it in php it gave me a good multi-dimensional assoc array.

Any suggestions on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒江雪… 2024-10-11 08:04:35

PHP 中的关联数组转换为 Map< /code> 在 Java 中。因此,在 Gson 看来,您的 JSON 格式为 Map,其中 Event 类具有字段 status时间

public class Event {
    private String Status;
    private String Time;

    // Add/generate getters, setters and other boilerplate.
}

是的,大写的字段名称很难看,但这就是 JSON 的样子。否则,您需要创建自定义 Gson解串器

以下是如何转换它。

Map<String, Event> events = new Gson().fromJson(json, new TypeToken<Map<String, Event>>(){}.getType());

A01A02 等成为 Map 键,其值成为 MapEvent 值代码>.您可能需要添加另一个自定义反序列化器以将 Time 转换为 java.util.Date

作为不同的替代方案,您还可以使用 Map> 代替。

An associative array in PHP translates to a Map in Java. So, in Gson's eyes, your JSON is in format of Map<String, Event> where the Event class has the fields status and time.

public class Event {
    private String Status;
    private String Time;

    // Add/generate getters, setters and other boilerplate.
}

Yes, capitalized field names are ugly, but that's how your JSON look like. You would otherwise need to create a custom Gson deserializer.

Here's how you can convert it.

Map<String, Event> events = new Gson().fromJson(json, new TypeToken<Map<String, Event>>(){}.getType());

A01, A02, etc become Map keys and its value becomes the Event value of Map. You may want to add another custom deserializer to get the Time into a java.util.Date.

As a different alternative, you can also use Map<String, Map<String, String>> instead.

与酒说心事 2024-10-11 08:04:35

该链接向我指出了一些我根本没有看过的东西。我还忘了在我的帖子中提到,了解 A01、A02 等非常重要。但是您在帖子中指出的一个链接引导我想出这个对我有用的方法。

JsonParser parse = new JsonParser();
JsonObject jobj = (JsonObject)parse.parse(status);                            
Set<Map.Entry<String, JsonElement>> map = jobj.entrySet();
Iterator<Map.Entry<String, JsonElement>> iterator = map.iterator();
int size = map.size();
for( int k = 0; k < size; k++ )
{
    Map.Entry<String, JsonElement> entry = iterator.next();
    String key = entry .getKey();
    JsonObject jele = (JsonObject)entry.getValue();
}

That link pointed me to something i wasn't even looking at. I also forgot to mention in my post that Knowing the A01, A02, etc is very important. But a link in the post that you pointed me to lead me to come up with this which works for me.

JsonParser parse = new JsonParser();
JsonObject jobj = (JsonObject)parse.parse(status);                            
Set<Map.Entry<String, JsonElement>> map = jobj.entrySet();
Iterator<Map.Entry<String, JsonElement>> iterator = map.iterator();
int size = map.size();
for( int k = 0; k < size; k++ )
{
    Map.Entry<String, JsonElement> entry = iterator.next();
    String key = entry .getKey();
    JsonObject jele = (JsonObject)entry.getValue();
}
凡间太子 2024-10-11 08:04:35

如果您的 json 略有不同,如下所示:

{
 [
  {"Status": "Ready", "Time": "00:00"}, 
  {"Status": "Ready", "Time": "00:00"}, 
  ......
 ]
}

Gson 将能够将 json 转换为对象集合,您必须自己定义该对象。所以你需要:

public class myAClass {
public String Status;
public Double time; //this could be a string/or even a Date I guess, 
                    //not sure what data you are expecting
                    //and the colon may cause a problem if parsed as a double
}

然后像这样使用它:

Type listType = new TypeToken<List<myAClass>>() {}.getType();
List<myAClass> myAClassList = new Gson().fromJson(json, listType); 
//where json is yr json string

然后你可以根据需要使用对象列表。

(进一步阅读此处

If your json was slightly different, like below:

{
 [
  {"Status": "Ready", "Time": "00:00"}, 
  {"Status": "Ready", "Time": "00:00"}, 
  ......
 ]
}

Gson would be able to convert the json into a collection of objects, the object you would have to define yourself. So you would need:

public class myAClass {
public String Status;
public Double time; //this could be a string/or even a Date I guess, 
                    //not sure what data you are expecting
                    //and the colon may cause a problem if parsed as a double
}

And then use that like so:

Type listType = new TypeToken<List<myAClass>>() {}.getType();
List<myAClass> myAClassList = new Gson().fromJson(json, listType); 
//where json is yr json string

You could then use the list of objects as you require.

(further reading over here)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文