gpt4 book ai didi

java - 无法从 [Source : java. io.StringReader@505fd738; 处的 START_OBJECT token 反序列化 java.util.ArrayList 实例;行 : 1, 列:1]

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

我在进行反序列化时遇到错误。

我有一个 URL,并且通过使用 sb 变量中的以下代码从该 URL 获取 JSON 数据,但如何使用此代码反序列化它?

我的 JSON 数据是:

 { "status": "success",
"msg": [
{ "fav_food": "roti",
"user_name": "123",
"email": "123@gmail.com" },
{ "fav_food": "iii",
"user_name": "343",
"email": "234@gmail.co" },
{ "fav_food": "paneer",
"user_name": "343",
"email": "343@gmail.com"}
]
}

代码:

package com.pack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@SuppressWarnings("serial")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Collection_to_jacksonJsonServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException {
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();

Map<String,Directory> contacts=new HashMap<String,Directory>();

ObjectMapper ob=new ObjectMapper();
List<Data> l=new ArrayList<Data>();
URL url=new URL("https://api-demo-py.appspot.com/getAllUsers");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
StringBuilder sb=new StringBuilder();
BufferedReader br=new BufferedReader(new
InputStreamReader(request.getInputStream()));
String line;
try
{
//read from the urlConnection via the bufferedReader
while ((line = br.readLine()) != null)
{
sb.append(line);
}
br.close();
}
catch(Exception e)
{
e.printStackTrace();
}

System.out.println(sb);
String jsonString=sb.toString();
l= ob.readValue(jsonString,new TypeReference<List<Data>>(){});

}
}

最佳答案

那么你必须反序列化为以下对象:

class Wrapper {
public String status;
public List<Data> msg;
}

关于java - 无法从 [Source : java. io.StringReader@505fd738; 处的 START_OBJECT token 反序列化 java.util.ArrayList 实例;行 : 1, 列:1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43876278/

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