我试图从下面给出的JSON中获取“photo_reference”中的字符串。但是,我得到了一个不匹配的例外。但我认为我做得对.Mismatch例外是结果,但可以清楚地看到结果是对象数组
{
"html_attributions" : [],
"results" : [
{
"name" : "Rhythmboat Cruises",
"photos" : [
{
,
"photo_reference" : "CnRnAAAAF-LjFR1ZV93eawe1cU_3QNMCNmaGkowY7CnOf-kcNmPhNnPEG9W979jOuJJ1sGr75rhD5hqKzjD8vbMbSsRnq_Ni3ZIGfY6hKWmsOf3qHKJInkm4h55lzvLAXJVc-Rr4kI9O1tmIblblUpg2oqoq8RIQRMQJhFsTr5s9haxQ07EQHxoUO0ICubVFGYfJiMUPor1GnIWb5i8",
"width" : 519
}
],
},
,
{
"name" : "Bucks Party Cruise",
"photos" : [
{
"photo_reference" : "CnRnAAAA48AX5MsHIMiuipON_Lgh97hPiYDFkxx_vnaZQMOcvcQwYN92o33t5RwjRpOue5R47AjfMltntoz71hto40zqo7vFyxhDuuqhAChKGRQ5mdO5jv5CKWlzi182PICiOb37PiBtiFt7lSLe1SedoyrD-xIQD8xqSOaejWejYHCN4Ye2XBoUT3q2IXJQpMkmffJiBNftv8QSwF4",
"width" : 800
}
],
},
以下是获取photo_reference的代码
var arr: JSONArray = JSONArray(result)
var jsonObject:JSONObject=arr.getJSONObject(0)
var array:JSONArray=jsonObject.getJSONArray("photos")
var obj:JSONObject=array.getJSONObject(0)
var photoref:String=obj.getString("photo_reference")
答案 0 :(得分:0)
您需要将根对象转换为JSONObject
var rootObject :JSONObject = JSONObject(result);
var arr: JSONArray = rootObject.getJSONArray("results")
var jsonObject:JSONObject=arr.getJSONObject(0)
var array:JSONArray=jsonObject.getJSONArray("photos")
var obj:JSONObject=array.getJSONObject(0)
var photoref:String=obj.getString("photo_reference")