JSON和Google Map很难 [英] hard time with JSON and google maps

查看:54
本文介绍了JSON和Google Map很难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个常见的问题,但是当脚本找不到属性时,我无法弄清楚如何防止解析出错...

It might be a frequent question but i cannot figure out how to prevent errors in my parsing when the script can't find a property...

很容易,因为即使空属性也像< location/>

in XML was easy because even the empty properties were like <location/>

但是现在如果 location 不可用,JSON paser找不到它,并导致错误...或

but now if location is not available JSON paser cant find it and it results in errors...OR

它可能会发生json具有不同的属性或孩子失去其父亲的情况...例如,如果您需要提取 SubityistrativeArea LocalityName >,但在 AddressLine ...

it may happen the json has different property or a children lost its father..... so for instance if you need to extract the LocalityName is no more under SubAdministrativeArea but under AddressLine...

你们有什么经验吗?解决和正确解析它的最佳方法是什么?

any of you have any experience about? what the best way to solve it and to parse it correctly?

推荐答案

在回答您的另一个问题时,我编写了以下javascript代码,以从maps api返回的JSON获取lat和lng,而无需对零进行任何验证结果.

While answering one of your other question I had written the following javascript code to obtain the lat and lng from the JSON returned by the maps api without any validations for zero results.

$.getJSON("getjson.php?address="+address,
        function(data){
            lat=data.results[0].geometry.location.lat;
            lng=data.results[0].geometry.location.lng;
                    //.....map initialization code      
       }    
    );

现在,如果我要验证零结果,则可以通过以下方式修改代码

Now if I were to validate for zero results, I'd modify the code in the following way

$.getJSON("getjson.php?address="+address,
            function(data){
                if (data.result.length>0) {
                          for (count=0;count<data.result.length;count++){
                             lat=data.results[count].geometry.location.lat;
                             lng=data.results[count].geometry.location.lng;
                           //.....map initialization code       
                           }
                        }
                }   
        );

如您所见,解析JSON很自然地存在于javascript和许多其他语言中,因为它解析为数组/列表和对象/字典/哈希.

As you can see parsing JSON comes naturally to javascript and to many other languages for it resolves down to arrays/lists and objects/dictionary/hash.

这篇关于JSON和Google Map很难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆