简体   繁体   English

如何在Android中解析没有标题对象的JSON?

[英]How to parse JSON without title object in Android?

I've a json output which returns something like this : 我有一个json输出,它返回如下内容:

[
 {
  "title":"facebook",
  "description":"social networking website",
  "url":"http://www.facebook.com"
 },
 {
  "title":"WoW",
  "description":"game",
  "url":"http://us.battle.net/wow/"
 },
 {
  "title":"google",
  "description":"search engine",
  "url":"http://www.google.com"
 }
]

I am familiar with parsing json having the title object, but i've no clue about how to parse the above json as it is missing the title object. 我熟悉解析具有标题对象的json,但是我不知道如何解析上述json,因为它缺少标题对象。 Can you please provide me with some hints/examples so i can check them and work on parsing the above code? 能否请您提供一些提示/示例,以便我检查它们并分析上面的代码?

Note : I've checked a similar example here but it doesn't have a satisfactory solution. 注意:我在这里检查了一个类似的示例,但是它没有令人满意的解决方案。

use JSONObject.has(String name) to check an key name exist in current json or not for example 例如,使用JSONObject.has(String name)检查当前json中是否存在键名

 JSONArray jsonArray = new JSONArray("json String");
 for(int i = 0 ; i < jsonArray.length() ; i++) {
   JSONObject jsonobj = jsonArray.getJSONObject(i);
   String title ="";
   if(jsonobj.has("title")){ // check if title exist in JSONObject

     String title = jsonobj.getString("title");  // get title
   }
   else{
        title="default value here";
    }

}
JSONArray jsonArr = new JSONArray(jsonResponse);

for(int i=0;i<jsonArr.length();i++){ 
JSONObject e = jsonArr.getJSONObject(i);
String title = e.getString("title");
}

Your JSON is an array of objects. 您的JSON是一个对象数组。

The whole idea around Gson (and other JSON serialization/deserialization) libraries is that you wind up with your own POJOs in the end. 围绕Gson (和其他JSON序列化/反序列化)库的整个想法是,最终您将拥有自己的POJO。

Here's how to create a POJO that represents the object contained in the array and get a List of them from that JSON: 以下是创建一个POJO的方法,该POJO表示数组中包含的对象,并从该JSON获取它们的List

public class App 
{
    public static void main( String[] args ) 
    {
        String json = "[{\"title\":\"facebook\",\"description\":\"social networking website\"," +
            "\"url\":\"http://www.facebook.com\"},{\"title\":\"WoW\",\"description\":\"game\"," +
            "\"url\":\"http://us.battle.net/wow/\"},{\"title\":\"google\",\"description\":\"search engine\"," +
            "\"url\":\"http://www.google.com\"}]";

        // The next 3 lines are all that is required to parse your JSON 
        // into a List of your POJO
        Gson gson = new Gson();
        Type type = new TypeToken<List<WebsiteInfo>>(){}.getType();
        List<WebsiteInfo> list = gson.fromJson(json, type);

        // Show that you have the contents as expected.
        for (WebsiteInfo i : list)
        {
            System.out.println(i.title + " : " + i.description);
        }
    }
}

// Simple POJO just for demonstration. Normally
// these would be private with getters/setters 
class WebsiteInfo 
{
    String title;
    String description;
    String url;
}

Output: 输出:

facebook : social networking website facebook:社交网站
WoW : game 魔兽:游戏
google : search engine 谷歌:搜索引擎

Edit to add: Because the JSON is an array of things, the use of the TypeToken is required to get to a List because generics are involved. 编辑以添加:由于JSON是事物的数组,因此涉及到TypeToken因此必须使用TypeToken才能到达List You could actually do the following without it: 没有它,您实际上可以执行以下操作:

WebsiteInfo[] array = new Gson().fromJson(json, WebsiteInfo[].class); 

You now have an array of your WebsiteInfo objects from one line of code. 现在,您WebsiteInfo一行代码WebsiteInfo拥有一个数组的WebsiteInfo对象。 That being said, using a generic Collection or List as demonstrated is far more flexible and generally recommended. 话虽如此,使用演示的通用CollectionList更加灵活,通常建议使用。

You can read more about this in the Gson users guide 您可以在Gson用户指南中阅读有关此内容的更多信息

JSONArray array = new JSONArray(yourJson);
for(int i = 0 ; i < array.lengh(); i++) {
JSONObject product = (JSONObject) array.get(i);
    .....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM