class Talk {
String[] values;
try {
InputStream is = getAssets().open("jdata.txt");
DataInputStream in = new DataInputStream(is);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read File Line By Line
while ((br.readLine()) != null) {
// Print the content on the console
strLine = strLine + br.readLine();
}
} catch (Exception e) { //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
parse(strLine);
}
public void parse(String jsonLine) {
Data data = new Gson().fromJson(jsonLine, Data.class);
values[0]= data.toString();
return;
}
}
這是jdata.txt
:java.lang.IllegalStateException:預期BEGIN_OBJECT錯誤
"{" + "'users':'john' + "}"
這是我Data.java
:
public class Data {
public String users;
}
我得到的錯誤是:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 9
任何人都可以對我而言,這個錯誤意味着什麼以及如何刪除它?編號:
我得到了答案。這些是我必須做的調整。首先,將字符串數組更改爲數組列表。
List<String> values = new ArrayList<String>();
下的調整是在這裏:
strLine = currentLine;
currentLine = br.readLine();
//Read File Line By Line
while (currentLine != null) {
// Print the content on the console
strLine = strLine + currentLine;
currentLine = br.readLine();
}
最後的調整在這裏:
String val = data.toString();
values.add(val);
代碼的某些部分可能是多餘的,但後來我會照顧那個。
你的json無效這是正確的json格式'{「users」:「john」 }' – RanRag 2012-01-26 22:51:14
@RanRag試過了。沒有工作。所以改變它試圖獲得正確的結果。 – Hick 2012-01-26 22:52:38