Google の GSON ライブラリを使用して Json を読み取るコードを数行まとめましたが、実行するとエラーが発生しました。それが何であるかはわかりませんが、jsonのフォーマット方法にあるのではないかと思います
エラー:
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(){
}
}
スーパーツイート
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
前もって感謝します!