error.org.jsonException.No“opening_hours”的值

时间:2018-06-12 07:54:28

标签: android json

我正在尝试获取对象“opening_hours”,但是我得到的是没有值的JSON异常

这是我的JSON

"results" : [
          {

             "icon":"https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
             "id" : "9328c5d6999dcbfc82acb7ec63df4df9f917731e",
             "name" : "Tapas Restaurant",
             "opening_hours" : {
                "open_now" : true,
                "weekday_text" : []
             }
          }
         ,
          {

         "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "10b0e9cea202572970aad34e9f252dac5e5220f8",
         "name" : "Laraib Inn",
         "opening_hours" : {
            "open_now" : false,
            "weekday_text" : []
         }  
}
]

这是我获取JSON的代码

var rootobject:JSONObject= JSONObject(response)
                    var objectarray:JSONArray=rootobject.getJSONArray("results")
                    for(i in 0..objectarray.length()-1){
                        var part:JSONObject=objectarray.getJSONObject(i)

                        var timepart:JSONObject=part.getJSONObject("opening_hours")

2 个答案:

答案 0 :(得分:0)

最佳解决方案是使用GSON库(序列化/反序列化库将Java对象转换为JSON并返回) - https://github.com/google/gson

然后创建具有以下属性的对象(包装器)(以及名称为&#34的嵌入对象; opens_hours" inside(在示例中名为B))。

所以对象示例:

A { icon (string), id (string), name (string), B { open_now (boolean) weekday_text (array) ... } }

答案 1 :(得分:0)

def obj_get(self, bundle, **kwargs):
    pk = kwargs['pk']
    if pk.startswith('identifier'):
        pk = pk.replace("identifier/", "")
        instance, created = Model.objects.get_or_create(identifier=pk)
        kwargs['pk'] = str(instance.pk)

    return super().obj_get(bundle, **kwargs)

尝试一下......