0

I am having a lot of tourble with this. I am trying to work on an updater and i am using an api that returns this from a url.: JSON

[
   {
      "downloadUrl":"URL",
      "fileName":"Name",
      "gameVersion":"Version",
      "name":"Name",
      "projectId":ID,
      "releaseType":"beta"
   },
   {
      "downloadUrl":"URL",
      "fileName":"Name",
      "gameVersion":"Version",
      "name":"Name",
      "projectId":ID,
      "releaseType":"beta"
   }
]

How can i get The Data out of this JSON returned by the URL. I do not want to use and "3rd Party" Parsers. Thanks. Also, i was stuck on this part:

I know i need to loop though an array, but there is no main array, unless it is "". ? That is what confused me. How can i parse this JSON from a url?

I saw someone did it like this, but idk if that will owrk in my JSON? Parsing JSON Object in Java

Community
  • 1
  • 1
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116

1 Answers1

1

In this case, your data is an array of objects, which can be stored to a HashMap object. Therefore we will retrieve each object in your array and add them into each HashMap. The HashMap works by using using a key to insert a value, i.e. HashMap<key type,value type>. To store a value with a key, you use HashMap.put(key,value) for example, map.put("downloadUrl", "URL")

// Remove the spacings yourself before trying the code
JSONArray array = new JSONArray("[
   {
      "downloadUrl":"URL",
      "fileName":"Name",
      "gameVersion":"Version",
      "name":"Name",
      "projectId":ID,
      "releaseType":"beta"
   },
   {
      "downloadUrl":"URL",
      "fileName":"Name",
      "gameVersion":"Version",
      "name":"Name",
      "projectId":ID,
      "releaseType":"beta"
   }
]");

List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>();
for(int i = 0 ; i < array.length() ; i++){
    HashMap<String,String> ht = new HashMap<String,String>();
    JSONObject o = json.getJSONObject(i);
    map.put("downloadUrl",o.getString("downloadUrl");
    map.put("fileName",o.getString("fileName");
    map.put("gameVersion",o.getString("gameVersion");
    map.put("name",o.getString("Name"));
    map.put("projectId",o.getString("projectId");
    map.put("releaseType",o.getString("releaseType");
    list.add(map);
}
Dennis
  • 58
  • 1
  • 6
  • Why bother putting it in a hash table when JSONObject *is* a hash table? – Hot Licks Oct 21 '13 at 01:33
  • And, one more thing: In the general case, an entry in a JSON object can be any valid JSON object -- not limited to strings. – Hot Licks Oct 21 '13 at 11:19
  • This is only an example for him to start. Optimally, he should create an object class on his own that contains all the attributes he need. However, as he mentioned, he is a beginner and Hashtable is just one object that he can readily use from the Java library. Also, Hashtable is not deprecated, as it still has uses due to it being synchronized while HashMap is not. – Dennis Oct 21 '13 at 12:31
  • Also, the reason I used hashtable in this case is because somebody commented on an answer that using hashtable is possible, and the OP asked how to implement it. – Dennis Oct 21 '13 at 12:38
  • This isn't subjective -- spec for Hashtable: "If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable." – Hot Licks Oct 22 '13 at 11:52