0
我把幾行代碼放在一起來閱讀使用Google的GSON Library的Json,但是當我運行它時收到錯誤。我不知道它是什麼,但我懷疑它可能是JSON是如何被格式化Json文件沒有被讀取 - Java/Gson
錯誤:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
at com.google.gson.Gson.fromJson(Gson.java:806)
at com.google.gson.Gson.fromJson(Gson.java:761)
at tweetfeedreader.main(tweetfeedreader.java:18)
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:306)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:795)
... 2 more
代碼:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class tweetfeedreader {
public static void main(String args[]) throws FileNotFoundException {
ArrayList<tweet> tCollection = new ArrayList<tweet>();
Gson gson = new Gson();
BufferedReader bufferedReader = new BufferedReader(new FileReader(
"C:/Users/DX029/Desktop/jsonfile.txt"));
Type type = new TypeToken<ArrayList<tweet>>(){}.getType();
ArrayList J_tweet = (ArrayList<tweet>)gson.fromJson(bufferedReader, type);
supertweet sup = new supertweet();
for(tweet t: sup.result){
System.out.println(t.text);
}
}
}
鳴叫
public class tweet {
String from_user;
String from_user_name;
String profile_image_url;
String text;
public tweet(){
}
}
supertweet
import java.util.ArrayList;
public class supertweet {
ArrayList<tweet> result;
public supertweet(){
result = new ArrayList<tweet>();
}
public void setResult(ArrayList<tweet> result)
{
result = result;
}
}
添加Json會佔用太多的空間,所以這裏是鏈接。 http://pastebin.com/rPrbfPMb
在此先感謝!
http://jsonlint.com你有一個問題瓦特/不帶引號的鑰匙了。 –