[英]Content is not allowed in prolog - parsing json
我正在嘗試使用json文件存儲虛擬Android Studio項目的用戶數據,並且嘗試測試從文件中讀取的LoginActivity時,出現錯誤
錯誤:任務':app:mergeDebugResources'的執行失敗。
/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"
}
]
}
這是我認為導致錯誤的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();
}
}
我已經在stackoverflow和Google上搜索了解決方案,但是大多數答案都與XML或JSON解組有關,我認為這與我正在做的事情無關,但我可能錯了。 提前致謝!
字節順序標記可能會阻止反序列化。 檢查您的編輯器沒有添加一個。
/ Users / james / projects / cwm / app / src / debug / res /values/users.json:1:1:錯誤:序言中不允許內容。
請將您的users.json從res文件夾移至資產文件夾,然后重試。
因為, getAssets()
方法引用assets
文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.