Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSONArray to String in javascript to send to java

I am working with an android app that uses phonegap. The java calls some HTML that displays a web page and the HTML uses javascript for functions. Now in the javascript I make a JSON call using parameters from the HTML. The problem is I need to send this JSONArray back to the Java class. However, when I use

JavaActivity.parseJson(jsonArray.toString());

(where jsonArray is the JSONArray and parseJson is the method in the java activity)

the string that it sends looks something like this:

[{Object:Object}, {Object:Object}, {Object:Object}, .....]

What am I doing wrong or is there another way to convert this String, or just send the JSONArray directly?

like image 387
Jay Avatar asked Jul 09 '12 16:07

Jay


People also ask

Can we convert JSON array to string?

Stringify a JavaScript ArrayUse the JavaScript function JSON. stringify() to convert it into a string.

Can we convert JSONArray to JSONObject?

We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method.

What is JSONArray in Java?

A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values. The internal form is an object having get and opt methods for accessing the values by index, and put methods for adding or replacing values.


1 Answers

Assuming jsonArray holds all the data you want to send to Java do:

var jString = JSON.stringify(jsonArray);

Now 'jString' is the json encoded string representation of your array.

like image 137
Simon MacDonald Avatar answered Oct 10 '22 16:10

Simon MacDonald