本文介绍了用org.Json用Java解析JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有很多困扰.我正在尝试使用更新程序,并且正在使用从URL返回此值的api:JSON

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"
   }
]

如何从URL返回的JSON中获取数据.我不想使用和"3rd Party"解析器.谢谢.另外,我被困在这部分上:

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:

我知道我需要遍历一个数组,但是没有主数组,除非它是". ?那就是让我困惑的地方.如何从URL解析此JSON?

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?

我看到有人这样做,但是idk是否可以在我的JSON中使用?使用Java解析JSON对象

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

推荐答案

在这种情况下,您的数据是一个对象数组,可以将其存储到HashMap对象中.因此,我们将检索数组中的每个对象,并将它们添加到每个HashMap中. HashMap通过使用键插入值(即HashMap<key type,value type>)来工作.要使用键存储值,请使用HashMap.put(key,value)例如map.put("downloadUrl", "URL")

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);
}

这篇关于用org.Json用Java解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:32
查看更多