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