2017-04-10 2 views
0

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(); 
    } 

} 

하는 XML과 관련된 또는 JSON 언 마샬링은 내가하고있는 일과 관련이 있다고 생각하지만 잘못 될 수 있습니다. 미리 감사드립니다!

+0

사용자 배열에 닫는 대괄호가 없습니다. 그것이 오류의 원인입니까? – BhalchandraSW

+0

@BhalchandraSW 복사 붙여 넣기에서 놓친 내용이 더 길었습니다. 이것은 더 긴 사용자 목록 중 처음 2 개입니다. 감사합니다. –

답변

0

/사용자/james/projects/cwm/app/src/debug/res /values/users.json:1:1 : 오류 : 프롤로그에서 내용을 사용할 수 없습니다.

res 폴더에서 assets 폴더로 users.json을 이동 한 다음 다시 시도하십시오.

getAssets() 메서드는 assets 폴더를 참조합니다.

0

byte order mark은 역 직렬화를 방지 할 수 있습니다. 에디터가 에디터를 추가하지 않는지 확인하십시오.

+0

저는 IntelliJ 플랫폼을 기반으로하는 Android Studio 텍스트 편집기를 사용하고 있습니다. BOM을 추가합니까? 어떻게 확인하고 해제 할 수 있습니까? –

+0

BOM (예 : Notepad ++)을 토글하여 텍스트가 있는지보고 해제 할 수있는 텍스트 편집기를 사용할 수 있습니다. Android Studio의 텍스트 편집기는 내게 어떤 문제도 부여하지 않았습니다. 또한 보이지 않는 문자가 있는지 확인하십시오. – etan

관련 문제