2014-04-26 2 views
1
내 JSON 문자열의 모습

, 문제 분석에 JSON 문자열

{ 
    "userList" : { 
     "user" : [{ 
       "Id" : "33", 
       "userId" : "raji2", 
       "customerId" : "35", 
       "username" : "raji2", 
       "status" : "1", 
       "locked" : "0", 
       "customAttributes" : "{\"uiPageId\":\"\",\"uiPageName\":\"\",\"uiPageViewMode\":\"Normal\"}", 
       "addressList" : { 
        "address" : "" 
       }, 
       "contactList" : { 
        "contact" : { 
         "contactno" : "+919526346097", 
         "email" : "[email protected]" 
        } 
       }, 
       "roleList" : { 
        "roleId" : "7" 
       }, 
       "privilegeList" : { 
        "privilegeId" : ["1", "10", "11", "12", "13", "14", "15", "16", "17", "23", "24", "25", "26", "27"] 
       }, 
       "entityList" : { 
        "entityId" : "" 
       } 
      }, { 
       "Id" : "34", 
       "userId" : "raji3", 
       "customerId" : "35", 
       "username" : "raji3", 
       "status" : "1", 
       "locked" : "0", 
       "customAttributes" : "{\"uiPageId\":\"\",\"uiPageName\":\"\",\"uiPageViewMode\":\"Normal\"}", 
       "addressList" : { 
        "address" : "" 
       }, 
       "contactList" : { 
        "contact" : { 
         "contactno" : "+919526346097", 
         "email" : "[email protected]" 
        } 
       }, 
       "roleList" : { 
        "roleId" : "7" 
       }, 
       "privilegeList" : { 
        "privilegeId" : ["1", "10", "11", "12", "13", "14", "15", "16", "17", "23", "24", "25", "26", "27"] 
       }, 
       "entityList" : { 
        "entityId" : "" 
       } 
      } 
     ] 
    } 
} 

나는이 아래의 샘플을 사용하여 이메일 또는 고객 ID를을 위해 노력 구문 분석하려고

는, 데이터가 코드, 널 (null)로오고있다 내가 시도 니펫을하는 것입니다 :

JSONObject obj = (JSONObject) new JSONParser().parse(data); 
    JSONObject jsonObject = (JSONObject) obj; 
    System.out.println(jsonObject); 
    String x=(String) jsonObject.get("customerId"); 
    System.out.println("Check Data"+x); 

나는 간단한 JSON 라이브러리를 사용했다. null 응답이 표시됩니다.

+0

음을,' "customAttributes는"의'_string_로 포함 된 JSON 개체는? 응? – fge

+0

어떤 특정 라이브러리를 사용하고 있습니까? 어떤 고객을 검색하려고합니까? 모두들? – Jahed

+0

@Jahed org.json.simple, 모두 –

답변

1

라이브러리를 사용하고 있지 않지만 "userList"필드를 먼저 가져 와서 "사용자"배열을 가져와 각각의 "customerId"를 얻기 위해 배열의 JSONObjects를 반복해야합니다.

1

2 번 구문 분석을 수행해야합니다. 아래 코드 이 작동하지 않습니다.
jsonObject.get ("userList"). get ("user");

하나씩 하나씩 사용하여 가장 안쪽 요소에 액세스하려고합니다.

3

다른 의견 작성자의 말처럼 루트 요소에서 직접 "customerId"에 액세스 할 수 없습니다. 당신은, 당신이 사용하고자하는 사용자 개체를 선택해야 다음 코드는 각 사용자 개체는 고객 ID를 속성이이

JSONObject obj = (JSONObject) new JSONParser().parse(data); 
System.out.println(obj); 
JSONObject userList = (JSONObject) obj.get("userList"); 
JSONArray user = (JSONArray) userList.get("user"); 
JSONObject userObj = (JSONObject) user.get(0); 
String customerId = (String) userObj.get("customerId"); 
System.out.println("Check Data " + customerId); 

때문에 수행

root -> userList -> user -> user[0] -> customerId 

가야 해요. 두 번째 사용자 객체를 사용하려면 5 번째 줄에 user.get(1);을 입력합니다.

편집 루프와 마지막 세 줄을 교체, 각 사용자에 대한 고객 ID를 얻으려면 :

JSONObject obj = (JSONObject) new JSONParser().parse(data); 
System.out.println(obj); 
JSONObject userList = (JSONObject) obj.get("userList"); 
JSONArray user = (JSONArray) userList.get("user"); 
for (Object userObj : user) { 
    JSONObject userJSONObject = (JSONObject) userObj; 
    String customerId = (String) userJSONObject.get("customerId"); 
    System.out.println("Check Data " + customerId); 
} 
+0

고마워요, 마침내 효과가 있었지만 모든 고객 ID를 얻는 데 관심이 있습니다 –

+0

마지막 세 줄을 간단한 루프로 바꾸면됩니다. 내 편집을 참조하십시오. – evanjdooner