gpt4 book ai didi

java - 如何使用gson将json转换为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 13:42:21 24 4
gpt4 key购买 nike

我正在尝试从 json 转换为 Java 对象。我收到一个包含书籍列表的 json 字符串:

[{"author":"Jonathan Khatz",
"id":8,
"name":"Criptography 6",
"publisher":"Chapman",
"year":2010},
{"author":"Hausi Muller",
"id":9,
"name":"SelfAdaptiveSystem",
"publisher":"UVic",
"year":2010}]

我创建了一个名为 Books 的对象:

package RESTful.client.model;

public class Books {
private int id;
private String name;
private String author;
private int year;
private String publisher;

public Books(){
}

public Books(int id, String name, String author, int year, String publisher){
this.id=id;
this.name=name;
this.author=author;
this.year=year;
this.publisher=publisher;
}

// public Getter & Setter methods
}

我还使用 doGet 方法以这种方式从 RESTful Web 服务检索此数据:

package RESTful.client.getBooks;
import RESTful.client.model.*;

// imports omitted

/**
* Servlet implementation class GetBooksByYear
*/
@WebServlet("/GetBooksByYear")
public class GetBooksByYear extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public GetBooksByYear() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Client client= Client.create();
//PrintWriter printWriter = response.getWriter();
String year= request.getParameter("year");
System.out.println("Year to search: "+year);
WebResource webResource= client.resource("http://localhost:8080/library/webapi/books/year/"+year);
ClientResponse rs=webResource.accept(
MediaType.APPLICATION_JSON_TYPE,
MediaType.APPLICATION_XML_TYPE).
get(ClientResponse.class);
System.out.println("Get operation response processing...\n");
String jsonBooks=rs.getEntity(String.class);

//kajacx solution:
Gson gson = new Gson();
Books[] booksA = gson.fromJson(jsonBooks, Books[].class);
List<Books> books = Arrays.asList(booksA);
for(Books book : books) {
System.out.println(book.getName()+", "+book.getAuthor()+", "+book.getAuthor()+", "+ book.getYear());
}

}

但是,我在执行时遇到错误:列出书籍 = new Gson().fromJson(jsonBooks, new TypeToken>(){}.getType());

下一个异常(exception):

java.lang.NoClassDefFoundError: com/google/gson/Gson
RESTful.client.getBooks.GetBooksByYear.doGet(GetBooksByYear.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

enter image description here

enter image description here

Java 列表的输出:密码学 6,​​乔纳森·哈茨,乔纳森·哈茨,2010SelfAdaptiveSystem,豪斯·穆勒,豪斯·穆勒,2010

json书籍格式为:[{"author":"Jonathan Khatz","id":8,"name":"Criptography 6","publisher":"Chapman","year":2010},{"author":"Hausi Muller","id":9,"name":"SelfAdaptiveSystem","publisher":"UVic","year":2010}]

干杯

最佳答案

我这样做了,因为这就是我一直使用 GSON 的方式:

Gson gson = new Gson();

String data = "[{\"author\":\"Jonathan Khatz\", \"id\":8,"
+ "\"name\":\"Criptography 6\","
+ "\"publisher\":\"Chapman\", \"year\":2010},"
+ "{\"author\":\"Hausi Muller\", \"id\":9,"
+ "\"name\":\"SelfAdaptiveSystem\","
+ "\"publisher\":\"UVic\", \"year\":2010}]";

Book[] books = gson.fromJson(data, Book[].class);
Arrays.asList(books).forEach(System.out::println);

而且效果很好。此外,我将 Books 重命名为 Book,因为该类仅代表一本书。

关于java - 如何使用gson将json转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131777/

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