-1
로 된 JSONObject로 데이터를 변환하는 방법

람다의 내 코드는AWS (λ)는 Node.js를에서 자바

dynamodb.query(params 
, function(err, data) { 
    if (err) { 
     context.done(err); 
    } 
    else { 
     console.log(JSON.stringify(data)); // successful response 
     var size = data.Items.length; 
     console.log("size:"+ size); 
     var rand = Math.floor(Math.random() * (size)); 
     console.log("rand" + rand); 
     var ret = data.Items[rand].content; 
     console.log(ret); 
     context.succeed(JSON.stringify(ret)); 
    } 

내 AWS 람다 로그 ("을 console.log (JSON.stringify (데이터));// 성공적인 응답 JSON.stringify (RET) ")

{"Items":[{"content":{"S":"content1"}},{"content":{"S":"content2"}}],"Count":2,"ScannedCount":2} 

그리고의 결과이다", "내 안드로이드 코드에서

{"S":"content1"} 

,이 API를 호출하고 바로 입술 보여 ult. 하지만 "java.lang.String을 JSONObject로 변환 할 수 없습니다."

Android에서 JSONObject로 변환하는 방법을 궁금합니다.

나는 문제는 당신이 getJSONObject을 요구하고있다 안드로이드

new JSONObject(notice).getJSONObject("S"); 
+1

번 확인하여'디버깅 변수 notice'. "변환 할 수 없습니다"오류는 일반적으로 구문 문제가있을 때 발생합니다. – donkon

+0

안녕하세요 @ 동콘 통지 변수가 "{"S ":"content1 "}"입니다. 그것은 틀리지 않습니다. 그래서 그것은 나를 미치게 만든다. 내가 무슨 소리를 더 많이 확인하니? – larsien

+0

답변을 게시했습니다 – donkon

답변

2

이 코드를 사용했다. 당신이 받고있는 객체는 String입니다.

{"S":"content1"} 
^JSON ^String 

당신은 방법 getJSONObjectget

JSONObject jsonNotice = new JSONObject("{\"S\":\"hello, world\"}"); 
String contentsOfS = jsonNotice.get("S"); 

를 호출해야하는 중첩 된 JSON 객체를위한 것입니다

{"S": {"foo": "bar"}} 
^JSON ^JSON 
+0

String contentsOfS = jsonNotice.getString ("S"); 이것이 내가 해결 한 것입니다! 대단히 감사합니다! – larsien