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

5
  • So what parser do you want to use? Commented Oct 20, 2013 at 23:02
  • In this case, i will parse it myself...with JSONArray, etc. Commented Oct 20, 2013 at 23:03
  • 1
    Think he wants to write his own Commented Oct 20, 2013 at 23:03
  • Not that hard, actually. I wrote one for Qt in about 500 lines of code -- took a day or so, IIRC. But of course I have 40 years experience programming. Commented Oct 20, 2013 at 23:07
  • @HotLicks Yea, the problem is, this is my second week of Java, and a like it so far. Commented Oct 20, 2013 at 23:16

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Why bother putting it in a hash table when JSONObject is a hash table?
And, one more thing: In the general case, an entry in a JSON object can be any valid JSON object -- not limited to strings.
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.
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.
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."

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.