Results 1 to 5 of 5
  1. #1
    JoeShearn is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default JSONArray from JSON that doesn't contain any names?

    Hi,

    I'm new to Java. How would I turn this JSON string in to a JSONArray?

    [{"Center":"10.337795657976551,-85.85095225274017","Name":"Playa Grande","URL":"http:\/\/jal.local\/playagrande\/"},{"Center":"0,0","Name":"Sandbox","URL":"htt p:\/\/jal.local\/sandbox\/"}]

    The JSON objects in the array don't have any names so I can't use getJSONArray("name")

    Thanks,

    Joe

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Not sure if you tried this, but getJSONArray can take an int parameter, which is the index between 0 and length()-1. JSONArray

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Your example looks like a JSONStore
    Java Code:
    [{"Center":"10.337795657976551,-85.85095225274017","Name":"Playa Grande","URL":"http:\/\/jal.local\/playagrande\/"},{"Center":"0,0","Name":"Sandbox","URL":"htt p:\/\/jal.local\/sandbox\/"}]
    This is what I do
    Java Code:
    JSONObject store_example = new JSONObject();
    JSONArray json_example = new JSONArray();
    JSONObject obj_example = new JSONObject();
    obj_example.put("Name", "Playa Grande");
    obj_example.put("URL", "http:\/\/jal.local\/playagrande\/");
    json_example.put(obj_example);
    store_example.put("Center",json_example);
    I do not know if this would work.. but you may be able to try

    Java Code:
    JSONArray json_example = new JSONArray();
    json_example.put("[{"Center":"10.337795657976551,-85.85095225274017","Name":"Playa Grande","URL":"http:\/\/jal.local\/playagrande\/"},{"Center":"0,0","Name":"Sandbox","URL":"htt p:\/\/jal.local\/sandbox\/"}]");
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    JoeShearn is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for all of the replies. If I try:

    Java Code:
    JSONArray json_example = new JSONArray();
    json_example.put("[{"Center":"10.337795657976551,-85.85095225274017","Name":"Playa Grande","URL":"http:\/\/jal.local\/playagrande\/"},{"Center":"0,0","Name":"Sandbox","URL":"htt p:\/\/jal.local\/sandbox\/"}]");
    Then I get an array with 12 elements in it. The first element contains the JSON text unchanged, the rest are all null.

    I'm running this on Android and getJSONArray doesn't seem to accept integer parameters.

    Any ideas?

    Thanks,

    Joe

  5. #5
    JoeShearn is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    I ended up just using GSON. Much simpler.

    Java Code:
    		Gson gson = new Gson();
    		locs = gson.fromJson(json, JALLocation[].class);
    Thanks for all of the help though,

    Joe

Similar Threads

  1. hashmap to json
    By prakashkadakol in forum New To Java
    Replies: 5
    Last Post: 07-30-2011, 01:32 AM
  2. Json
    By mac in forum New To Java
    Replies: 10
    Last Post: 06-08-2010, 11:15 PM
  3. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  4. Put JSONObject to JSONArray?
    By DigitalMan in forum Advanced Java
    Replies: 3
    Last Post: 04-03-2010, 09:04 AM
  5. JSON Parsing
    By prakashkadakol in forum New To Java
    Replies: 1
    Last Post: 01-04-2010, 07:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •