解析 JSON 为 Java 对象:预期为 BEGIN_OBJECT,但实际是 BEGIN_ARRAY。

huangapple go评论99阅读模式
英文:

Parse json to java object: Expected BEGIN_OBJECT but was BEGIN_ARRAY

问题

以下是翻译好的内容:

有人能帮我解析 JSON 字符串为对象吗?我在尝试,但总是得到这样的错误:

预期是 BEGIN_OBJECT,但在第1行第3列处却是 BEGIN_ARRAY,路径为 $[0]

以下是我的代码:

Item 类:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Item {

    @JsonProperty("id")
    String id;

    @JsonProperty("dTime")
    long dTime;

    @JsonProperty("aTime")
    long aTime;
}

将 JSON 转换为对象:

String jsonString = "[
  	[
  		{
  			\"id\":\"string\",
          	\"dTime\": 1111111111,
          	\"aTime\": 1111111111
  		},
  		{
  			\"id\":\"string\",
          	\"dTime\": 1111111111,
          	\"aTime\": 1111111111
  		}
	]
]";
Gson gson = new Gson();
Type listType = new TypeToken<List<Item>>() {}.getType();
List<Item> list = gson.fromJson(jsonString, listType);

System.out.println(gson.toJson(list));

**更新:**
因为我从外部 API 得到这个 JSON 作为响应:

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

是否可能在这个 restTemplate.exchange(..) 内部将其转换为 Item 对象的数组?还是使用 gson 的方式更好?
英文:

Can someone help me with parsing json string to object, I´m trying but always get error like this:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0]

Here is my code:

Item class:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Item{

	@JsonProperty(&quot;id&quot;)
	String id;
	
	@JsonProperty(&quot;dTime&quot;)
	long dTime;
	
	@JsonProperty(&quot;aTime&quot;)
	long aTime;
}

converting json to object:

String jsonString = &quot;[
  	[
  		{
  			&quot;id&quot;:&quot;string&quot;,
          	&quot;dTime&quot;: 1111111111,
          	&quot;aTime&quot;: 1111111111
  		},
  		{
  			&quot;id&quot;:&quot;string&quot;,
          	&quot;dTime&quot;: 1111111111,
          	&quot;aTime&quot;: 1111111111
  		}
	]
]&quot;;
Gson gson = new Gson();
Type listType = new TypeToken&lt;List&lt;Item&gt;&gt;() {}.getType();
List&lt;Item&gt; list = gson.fromJson(jsonString, listType);

System.out.println(gson.toJson(list));

Update:
Because i get this json as response from external API:

ResponseEntity&lt;String&gt; response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);

Is it possible to make this converting to Array of Item objects inside this restTemplate.exchange(..) or this with gson is better way?

答案1

得分: 1

您可以使用 ParameterizedTypeReference 来直接映射 Java 列表对象,并且由于您的数据包装在 [] 中,可以使用 List<List<Item>>

ParameterizedTypeReference<List<List<Item>>> responseType =
                        new ParameterizedTypeReference<List<List<Item>>>() {};
ResponseEntity<List<List<Item>>> response = 
                        restTemplate.exchange(url, HttpMethod.POST, request, responseType);
英文:

You can use ParameterizedTypeReference to directly map in java list object and use List&lt;List&lt;Item&gt;&gt; since your data wrap in []

ParameterizedTypeReference&lt;List&lt;List&lt;Item&gt;&gt;&gt; responseType =
                    new ParameterizedTypeReference&lt;List&lt;List&lt;Item&gt;&gt;&gt;() {};
ResponseEntity&lt;List&lt;List&lt;Item&gt;&gt;&gt; response = 
                    restTemplate.exchange(url, HttpMethod.POST, request, responseType);

答案2

得分: 0

你的jsonString多了一个'['。应该是

String jsonString = &quot;
[
    {
        &quot;id&quot;:&quot;string&quot;,
        &quot;dTime&quot;: 1111111111,
        &quot;aTime&quot;: 1111111111
    },
    {
        &quot;id&quot;:&quot;string&quot;,
        &quot;dTime&quot;: 1111111111,
        &quot;aTime&quot;: 1111111111
    }
]
&quot;;
英文:

Your jsonString has an extra '['. It should be

String jsonString = &quot;
[
    {
        &quot;id&quot;:&quot;string&quot;,
        &quot;dTime&quot;: 1111111111,
        &quot;aTime&quot;: 1111111111
    },
    {
        &quot;id&quot;:&quot;string&quot;,
        &quot;dTime&quot;: 1111111111,
        &quot;aTime&quot;: 1111111111
    }
]
&quot;;

huangapple
  • 本文由 发表于 2020年10月26日 16:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64533579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定