2011-10-06 5 views
0

JSON 문자열 :안드로이드 구문 분석 JSON 파일

{conf:{"quantity_uom":"l",price_uom:"euro",distance_uom:"km",consumption_uom:"km/l"}} 

코드 : 나는 No value for quantity_uom을 돌아왔다

try{ 
     if (str!=""){ 
      this.json = new JSONObject(str); 
      this.quantityUom=this.json.getString("quantity_uom"); 
      this.distanceUom=this.json.getString("distance_uom"); 
      this.priceUom=this.json.getString("price_uom"); 
      this.consumptionUom=this.json.getString("consumption_uom");    
     } 
    }catch (Exception e) { 
     throw e; 
    } 

. 나는 어떻게 잘못하고 있니? String은 json 텍스트를 포함합니다.

정말 고마워요.

+0

'str! = ""'Java/Android가 작동하지 않습니다. 이것을 사용하십시오 :'! str.equals ("")' – WarrenFaith

+0

문자열이 유효한 JSON이 아닙니다. – JeremyP

답변

4
try{ 
    if (!str.equals("")){ 
     Json obj = new JSONObject(str); 
     this.json = obj.getJSONObject("conf"); 
     this.quantityUom=this.json.getString("quantity_uom"); 
     this.distanceUom=this.json.getString("distance_uom"); 
     this.priceUom=this.json.getString("price_uom"); 
     this.consumptionUom=this.json.getString("consumption_uom");    
    } 
}catch (Exception e) { 
    throw e; 
} 

먼저 다른 값을 가져 문자열에서 그 JSON 객체에서 'conf의'라는 이름의 JSON 개체를 얻을 수 있습니다.

1

JSON 문자열이 올바르지 않습니다.

이 시도 :

{"conf":{"quantity_uom":"l","price_uom":"euro","distance_uom":"km","consumption_uom":"km/l"}}