无法获取与正确的格式JSON对象 [英] can't fetch json object with correct format
问题描述
假如我从一个API获取一些 JSON
数组等等
我想检索该 JSON
为一些
对象 URL_Some
网址
公共类的一些实现Serializable { 私人字符串ID;
私人字符串称号;
私人字符串描述;
私人字符串投票;
私人字符串created_at;
私人字符串的updated_at;
} //所有的getter和setter
公开名单<有的> getSome()抛出IOException
尝试{
HTT prequest请求=执行(HTT prequest.get(URL_Some));
SomeWrapper响应= fromJson(请求,SomeWrapper.class);
现场[]栏= response.getClass()getDeclaredFields()。
的for(int i = 0; I< fields.length;我++)
{
尝试{
Log.i(TAG,(串)域[I]获得(响应));
}赶上(IllegalAccessException E){
e.printStackTrace(); //改变catch语句使用文件的身体|设置|文件模板。
}
Log.i(TAG,字段[I] .getName());
}
如果(响应= NULL&放大器;!&安培;!response.results = NULL)
返回response.results;
返回Collections.emptyList();
}赶上(HTT prequestException E){
扔e.getCause();
}
}
和 SomeWrapper
只是
私有静态类SomeWrapper { 私人列表<有的>结果;
}
问题是,我不断收到此消息。
java.lang.IllegalStateException:预期BEGIN_OBJECT但BEGIN_ARRAY
PS:我用
进口com.google.gson.Gson;进口com.google.gson.GsonBuilder;进口com.google.gson.JsonParseException;
您应该JSON实际上是这样的:
{成果: [{\"id\":1,\"title\":\"title\",\"description\":\"description\",\"vote\":null,\"created_at\":\"2013-11-12T21:08:10.922Z\",\"updated_at\":\"2013-11-12T21:08:10.922Z\"}]}$c$c>
GSON将尝试解析JSON并创建一个SomeWrapper对象。仅此一点就告诉他GSON将等待这种格式 {...}
A JSON,因为他期待的对象。然而,你传递一个数组来代替,这就是为什么它抱怨期待BEGIN_OBJECT( {
),但越来越BEGIN_ARRAY代替( [
) 。在此之后,它会想到这个JSON对象有一个结果
字段,这将持有对象的数组。
您可以创建列表<有的>
直接,而不需要一但包装类。这样做,这样做,而不是:
键入类型=新TypeToken<名单,LT;有的>>(){} .getType();
清单<有的> someList =新GsonBuilder()创建()fromJson(jsonArray,类型)。;
在这种情况下,您可以使用您发布的原始JSON阵列。
Suppose I am fetching from an api some json
array as so
[{"id":1,"title":"title","description":"description","vote":null,"created_at":"2013-11-12T21:08:10.922Z","updated_at":"2013-11-12T21:08:10.922Z"}]
I want to retrieve this json
as an Some
object from URL_Some
url
public class Some implements Serializable {
private String id;
private String title;
private String description;
private String vote;
private String created_at;
private String updated_at;
}//with all getters and setters
.
public List<Some> getSome() throws IOException {
try {
HttpRequest request = execute(HttpRequest.get(URL_Some));
SomeWrapper response = fromJson(request, SomeWrapper.class);
Field[] fields = response.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
try {
Log.i("TAG", (String) fields[i].get(response));
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Log.i("TAG", fields[i].getName());
}
if (response != null && response.results != null)
return response.results;
return Collections.emptyList();
} catch (HttpRequestException e) {
throw e.getCause();
}
}
and SomeWrapper
is simply
private static class SomeWrapper {
private List<Some> results;
}
The problem is that I keep on getting this message
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
PS : I use
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
Your json should actually be like this:
{"results": [{"id":1,"title":"title","description":"description","vote":null,"created_at":"2013-11-12T21:08:10.922Z","updated_at":"2013-11-12T21:08:10.922Z"}]}
Gson will try to parse the json and create a SomeWrapper object. This alone tells Gson he will wait for a json with this format {...}
since he's expecting an object. However you passed an array instead, that's why it complains about expecting BEGIN_OBJECT ({
) but getting BEGIN_ARRAY instead ([
). After that, it will expect this json object to have a results
field, which will hold an array of objects.
You can create List<Some>
directly without the need of a wrapper class however. To do so do this instead:
Type type= new TypeToken<List<Some>>() {}.getType();
List<Some> someList = new GsonBuilder().create().fromJson(jsonArray, type);
In this case, you can use the original json array you posted.
这篇关于无法获取与正确的格式JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!