java 是否有像 LitJSON 这样的库?

Is there any library like LitJSON for java?

前段时间我正在开发一个 android 应用程序,其中有很多 JSON 请求,所有响应都直接从 JSON 对象中使用。

现在我正在开发一个也需要 JSON 请求的 C# 项目,为此我使用了 LitJSON 库,它会根据响应自动创建一个新对象。 LitJSON 文档中的示例:

我可以class喜欢:

public class Person{
    public string   Name     { get; set; }
    public int      Age      { get; set; }
    public DateTime Birthday { get; set; }
}

并且使用 JSON 字符串可以是:

string json = @"
            {
                ""Name""     : ""Thomas More"",
                ""Age""      : 57,
                ""Birthday"" : ""02/07/1478 00:00:00""
            }";

我能做到:

Person thomas = JsonMapper.ToObject<Person>(json);
Console.WriteLine("Thomas' age: {0}", thomas.Age);

输出:

Thomas' age: 57

java有这样的库吗?

我认为你应该试试 Gson。

https://sites.google.com/site/gson/gson-user-guide

Gson 非常适合这个:

String json = gson.toJson(personA);    
Person personB = gson.fromJson(json, Person.class);

Jackson 是一个非常流行的 JSON 库,Spring Boot 和 Dropwizard 也将其用作首选库。

正如其他人所提到的,GSON 也是一个不错的选择。

如果可以的话,Groovy 的 JsonSlurper 也很强大。当 Spring/Dropwizard 不在图片中时,我们会在我广泛工作的地方使用它 :)