Reputation: 8920
I need to convert JSON generated in JavaScript to a JSONArray in Java - the context here is a Cordova plugin for a hybrid Android app. The following string
"[\"9784142605765949528\",2869,264,7]"
causes Java to complain. It comes back with the message
["9784142605765949528",2869,264,7] of type java.lang.String cannot be converted to JSONArray
I fail to see why this might be happening. The first array element above is a string though it looks like a number. This is because I am using a custom 64 bit integer implementation in JS which returns its value as a string.
The part of my Java code that is parsing this JSON is as follows
try
{
Feedback.postBackInfo("LWPD4 " + lwpd[4]);
JSONArray hXYZ = new JSONArray(lwpd[4]);
Feedback
is an internal utility I use to aid with debugging. The JSON I show below comes from that call to Feedback
and it is the subsequent new JSONArray()
that is throwing this error
I fail to see what is wrong here - the string is valid JSON.
Upvotes: 0
Views: 515
Reputation: 13600
I believe you might be confused about the input String you are trying to parse. The following codes works just fine.
import org.json.JSONArray;
public class JsonTest {
public static void main(String[] args) throws Exception {
String json = "[\"9784142605765949528\",2869,264,7]";
JSONArray result = new JSONArray(json);
System.out.println(result);
}
}
Since the above works I suspect that you are not trying to parse the String you think you are trying to parse. What is the actual value of your lwpd[4]
variable?
Upvotes: 1