[英]Content is not allowed in prolog - parsing json
I'm attempting to use a json file to store user data for a dummy Android Studio project, and while trying to test the LoginActivity which reads in the file, I'm getting an error 我正在尝试使用json文件存储虚拟Android Studio项目的用户数据,并且尝试测试从文件中读取的LoginActivity时,出现错误
Error:Execution failed for task ':app:mergeDebugResources'. 错误:任务':app:mergeDebugResources'的执行失败。
/Users/james/projects/cwm/app/src/debug/res/values/users.json:1:1: Error: Content is not allowed in prolog.
/Users/james/projects/cwm/app/src/debug/res/values/users.json:1:1:错误:序言中不允许内容。
{
"users": [
{
"name": "James",
"email": "j@gmail.com",
"address": "addr1",
"password": "password",
"usertype": "USER"
},
{
"name": "Kayla",
"email": "k@gmail.com",
"address": "addr1",
"password": "password",
"usertype": "MANAGER"
}
]
}
And here is the code in the LoginActivity that I believe is causing the error: 这是我认为导致错误的LoginActivity中的代码:
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = this.getAssets().open("users.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private void parseJ() {
try {
JSONObject jsonObject = new JSONObject(loadJSONFromAsset());
if(jsonObject != null) {
JSONArray jsonArray = jsonObject.getJSONArray("users");
if(jsonArray != null) {
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
if(jo != null) {
String name = (String) jo.get("name");
String email = (String) jo.get("email");
String address = (String) jo.get("address");
String password = (String) jo.get("password");
String userType = (String) jo.get("usertype");
User u = new User(name, email, address, password);
u.setUserType(userType);
userList.put(email, u);
a.setMessage(u.toString());
a.show();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I've searched stackoverflow and Google for a solution but most answers pertain to XML or JSON unmarshalling, which I don't believe is relevant to what I'm doing but I could be wrong. 我已经在stackoverflow和Google上搜索了解决方案,但是大多数答案都与XML或JSON解组有关,我认为这与我正在做的事情无关,但我可能错了。 Thanks in advance!
提前致谢!
It's possible that a byte order mark prevents deserialization. 字节顺序标记可能会阻止反序列化。 Check that your editor doesn't add one.
检查您的编辑器没有添加一个。
/Users/james/projects/cwm/app/src/debug/ res /values/users.json:1:1: Error: Content is not allowed in prolog. / Users / james / projects / cwm / app / src / debug / res /values/users.json:1:1:错误:序言中不允许内容。
Please move your users.json from res folder to assets folder and try again. 请将您的users.json从res文件夹移至资产文件夹,然后重试。
Because, getAssets()
method refers to assets
folder. 因为,
getAssets()
方法引用assets
文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.