Question

1) I've got a JSON file:

{
  "serverURI":"http://localhost:8080/PocketUNI_API/serverURLs",
  "newsURI":"http://localhost:8080/gateway/rss/rss.xml",
  "modulesURI":"http://localhost:8080/PocketUNI_API/modules"
}

2) I need to get URLs on Java client in String format.

String json = jsonReceiver.makeHttpRequest(URL_SERVER, "GET", params);
JSONArray uris = new JSONArray(json);

Receiver works fine and json shows the correct string received, but when it goes to parsing with JSONArray it throws an error

org.json.JSONException: Value {"serverURI":"http:\/\/192.168.0.... of type org.json.JSONObject cannot be converted to JSONArray. 

Question: How to parse json with URL values?

Was it helpful?

Solution

You don't get a JSONArray but a JSONObject.

JSONObject uris = new JSONObject(json);

OTHER TIPS

json is a json object not an array, that is why you are getting the error. An array will be wrapped with in [ and ], and objects within { and }.

JSONObject uris = new JSONObject (json);

Instead of JSONArray you must use JSONObject.

JSONObject uris = new JSONObject(json);

In your code just replace JSONArray to JSONobject

JSONObject uris = new JSONObject(json);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top