larsien larsien - 1 year ago 132
Node.js Question

How to convert data from AWS lambda node.js to JSONObject in Java

My code snippet of lambda is

dynamodb.query( params
, function(err, data) {
if (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;

My AWS Lambda log of ("console.log(JSON.stringify(data)); // successful response") is


And result of "JSON.stringify(ret)" is


In my Android code, call this API and just show the result.
But it only showed "java.lang.String cannot be converted to JSONObject"

I wonder how to convert to JSONObject in Android.

I used this code in Android

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

Answer Source

The problem is you are calling getJSONObject. The object you are getting is a String.

^JSON    ^String

You should be calling get

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

The method getJSONObject is for nested JSON objects:

{"S": {"foo": "bar"}}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download