jsonファイルを使用してダミーのAndroid Studioプロジェクトのユーザーデータを保存しようとしていますが、ファイルを読み込んでいるLoginActivityをテストしようとしています。エラープロローグでjsonを解析する際にコンテンツが許可されていません
エラー: ':app:mergeDebugResources'タスクの実行に失敗しました。
/Users/james/projects/cwm/app/src/debug/res/values/users.json:1:1: Error: Content is not allowed in prolog.
{
"users": [
{
"name": "James",
"email": "[email protected]",
"address": "addr1",
"password": "password",
"usertype": "USER"
},
{
"name": "Kayla",
"email": "[email protected]",
"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のアンマーシャリングは、私がやっていることとは関係ないと信じていますが、私は間違っている可能性があります。前もって感謝します!
ユーザーアレイに閉じ括弧がありません。それがエラーの原因ですか? – BhalchandraSW
@BhalchandraSW私はコピー貼り付けでそれを逃した、これはユーザーの長いリストの最初の2だけです。おかげで –