0

I'm trying to parse some information but I get some errors. This is part of my code:

public static ArrayList<Advertiser> negociosCiudad(String ciudad){
    ArrayList<Advertiser> result = new ArrayList<Advertiser>();
    JSONParser jp = new JSONParser();
    JSONArray jArray = jp.getJSONFromUrl(URL + "searchByCity/" + ciudad);
    try {
        for (int i = 0; i < jArray.length(); i++) {

            Advertiser a = new Advertiser();
            JSONObject o = jArray.getJSONObject(i);
            a.setDireccion(o.getString(TAG_ADDRESS));
            a.setId(o.getString(TAG_ADVERTISER_ID));
            a.setCiudad(o.getString(TAG_CITY));
        ...
        }
}

This is what I'm receiving:

["Hello","Good Bye","Errors","Everywhere","Help", ...]

And this is the error I'm getting:

org.json.JSONException: Value Hello at 0 of type java.lang.String cannot be converted to JSONArray

I haven't figured out how to do this correctly, but I believe it's something simple. I realized that I'm not using JSON arrays, but I don't know what to do.

I'll be editing this post with the things I'm trying to do to fix it.

Thank you in advance.

EDIT

I had a mistake and put a different response, let me get the real one since ["Hello","Good Bye","Errors","Everywhere","Help", ...] is from another query –

3
  • what you did receive is a JSONArray, and contains Strings Commented Jun 11, 2015 at 20:27
  • That json doesn't make sense for what you are tying to do. Are you sure your json is valid? From your code your actually want something that looks like {"TAG_ADDRESS":"1234 Some Rd", "TAG_ADVERTISER_ID":"546", "TAG_CITY":"City"} Commented Jun 11, 2015 at 20:34
  • I had a mistake and put a different response, let me get the real one since ["Hello","Good Bye","Errors","Everywhere","Help", ...] is from another query Commented Jun 11, 2015 at 21:04

1 Answer 1

1

You need an object which holds your array, like this:

{
 "arrayOfIntegers": [
   1,
   2,
   3,
   4,
   5
 ]
}

Anyway, personally, if you aren't work with big Jsons, and you dont look for maximum performence you could simply use Gson.

You can fetch it from maven at "com.google.code.gson".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.