Android 使用可选字段解析JSON

Android 使用可选字段解析JSON,android,json,parsing,field,optional,Android,Json,Parsing,Field,Optional,我试图在android studio中解析JSON,它包含以下内容: "stops": [ { "num": 1, "time": "2016-04-27T06:15:00.000Z", "title":"Flight to London", "desc":"Barcelona BCN-London-Gatwick LGW", "type":"0", "subtype":0 },

我试图在android studio中解析JSON,它包含以下内容:

"stops": 
[
    {
        "num": 1,
        "time": "2016-04-27T06:15:00.000Z",
        "title":"Flight to London",
        "desc":"Barcelona BCN-London-Gatwick LGW",
        "type":"0",
        "subtype":0
    },
    {
        "num": 2,
        "time": "2016-04-27T10:35:00.000Z",
        "title":"Gatwick express",
        "desc":"From Airport to London",
        "type":"0",
        "subtype":1
    },
    {
        "num": 3,
        "time": "2016-04-27T12:15:00.000Z",
        "title":"Pub the black horse",
        "desc":"From Airport to London",
        "type":1,
        "subtype":1,
        "location": "51.476334, -0.062700",
        "images": [                 "https://fitzrovianews.files.wordpress.com/2011/01/black_horse_rathbone_pl.jpg" 
        ]               
    },
    {
        "num": 4,
        "time": "2016-04-27T12:16:47.000Z",
        "title":"The Tower Bridge",
        "desc":"# The Tower Bridge Facts\n## Architecture\n**Tower Bridge** is a combined bascule and suspension bridge in London built in _1886–1894_. The bridge crosses the River Thames close to the Tower of London and has become an iconic symbol of London. Tower Bridge is one of five London bridges now owned and maintained by the Bridge House Estates, a charitable trust overseen by the City of London Corporation. \n>It is the only one of the Trust's bridges not to connect the City of London directly to the Southwark bank, as its northern landfall is in Tower Hamlets.\n## The bridge Exhibition\nThis must-see London attraction invites you to step inside the most famous bridge in the world to explore its iconic structure, spectacular views and glass floor, modern exhibitions and magnificent Victorian Engine Rooms! ",
        "type":1,
        "subtype":6,
        "location": "51.507792, -0.087786",
        "images": [                 
"https://i.ytimg.com/vi/nby0Mr2LfBQ/hqdefault.jpg",
"http://raindropsofsapphire.com/wp-content/uploads/2011/10/london-bridge.jpg",
"http://www.londonforfree.net/gizmo/wp-content/uploads/2015/02/southwark-bridge.jpg"
        ]               
    },
    {
        "num": 5,
        "time": "2016-04-27T12:18:10.000Z",
        "title":"St. Paul Cathedral",
        "desc":"# HISTORY \nSt **Paul's Cathedral**, London, is an _Anglican cathedral_, the seat of the _Bishop of London_ and the mother church of the Diocese of London.  \n * It sits on Ludgate Hill at the highest point of the City of London and is a Grade 1 listed building. \n * Its dedication to Paul the Apostle dates back to the original church on this site, founded in AD 604.",
        "type":1,
        "subtype":6,
        "location": "51.513825, -0.098351",
        "images": [                 
"https://d1wgio6yfhqlw1.cloudfront.net/sysimages/product/resized6/Interior_St_Pauls_Cathedral_132_12992.jpg",
"https://d1kioxk2jrdjp.cloudfront.net/resized/486x324/48-st_pauls_ctahedral_millenirm_bridge.jpg",
"http://i4.mirror.co.uk/incoming/article8299330.ece/ALTERNATES/s615b/LOND-2016-052-HMQ-St-Pauls-Thanks-Giving-704JPG.jpg"
        ]               
    }
] 
问题是,我不知道如何处理可选的字段位置或图像。我知道如何处理第一站,我正在这样做:

JSONArray stops = jsonObj.getJSONArray("stops");

        for (int i = 0; i < stops.length(); i++) {
            JSONObject c = stops.getJSONObject(i);

            String num = c.getString("num");
            String time = c.getString("time");
            String title = c.getString("title");
            String descripcion = c.getString("desc");
            String type = c.getString("type");
            String subtype = c.getString("subtype");
......

}
但我不知道如何在这里检查它是elment位置还是jsonArray图像…

使用方法

使用方法


你应该使用改型和谷歌Gson,在那里你不需要做很多工作, 他们将为你做这项工作。 看看这些


你应该使用改型和谷歌Gson,在那里你不需要做很多工作, 他们将为你做这项工作。 看看这些


处理JSON中可选字段的最佳方法是使用opt而不是get

如果存在,opt提供解析值;如果请求的键不存在,opt提供该数据类型的默认值

最好的是,它甚至不需要try-catch块,因为它总是返回一个值,并且在服务器出现任何错误的情况下,它不会让您的应用程序崩溃或阻止解析其他值

String location = response.optString("location");

如果响应中存在位置,则它将使用值初始化,或者将字符串保留为null。对于int或long,默认值为0,对于boolean,默认值为false。阅读关于opt的更多详细信息。

处理JSON中可选字段的最佳方法是使用opt而不是get

如果存在,opt提供解析值;如果请求的键不存在,opt提供该数据类型的默认值

最好的是,它甚至不需要try-catch块,因为它总是返回一个值,并且在服务器出现任何错误的情况下,它不会让您的应用程序崩溃或阻止解析其他值

String location = response.optString("location");

如果响应中存在位置,则它将使用值初始化,或者将字符串保留为null。对于int或long,默认值为0,对于boolean,默认值为false。了解更多详情,请选择。

太棒了!首先检查,然后初始化。为什么不使用optString、optInt或optJson等呢?@MohammedAtif如果它们都是实现所需功能的好方法,但问题是我只使用optString,例如,当我想要获取键的值(如果键存在)或默认值(空字符串)时,如果密钥不存在,但您当然可以在此处使用它来了解密钥是否存在!在这里,我不认为任务涉及检查密钥的存在。他想处理这件事,太棒了!首先检查,然后初始化。为什么不使用optString、optInt或optJson等呢?@MohammedAtif如果它们都是实现所需功能的好方法,但问题是我只使用optString,例如,当我想要获取键的值(如果键存在)或默认值(空字符串)时,如果密钥不存在,但您当然可以在此处使用它来了解密钥是否存在!在这里,我不认为任务涉及检查密钥的存在。在这里,他想处理它。