0
Google의 GSON 라이브러리를 사용하여 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(){
}
}
수퍼바이벌
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 당신은 비 인용 키/w도 문제가있는 것입니다. –