在javascript中读取JSON数据 [英] reading JSON data in javascript

查看:50
本文介绍了在javascript中读取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JSON对象

{"stores":"{
               "1":{
                     "name":"Publix",
                   "address":"1fbdfhbdhsdhsrh",
                   "long":"-84.012502",
                   "lat":"33.878952"},
               "2":{
                     "name":"Publix",
                    "address":"fgsregerge",
                     "long":"-84.125147",
                    "lat":"33.9448"}
          }"

}

这是在jquery.ajax调用中以数据类型:json返回的。我可以访问data.stores并显示警告框中的所有商店,但data.stores.1不起作用..
如何正确阅读?

this is returned in a jquery.ajax call as datatype:json. I am able to access data.stores and that displays all the stores in alert box but data.stores.1 does not work.. How can I read this properly?

推荐答案

您可以使用 data.stores [1] ,但实际上您似乎在表示一个数组非常奇怪。

You could use data.stores["1"], but really you seem to be representing an array in a really odd way.

你应该只使用一个数组:

You should just use an array instead:

{
    "stores": [
        {
            "name": "Publix",
            "address": "1fbdfhbdhsdhsrh",
            "long": "-84.012502",
            "lat": "33.878952"
        },
        {
            "name": "Publix",
            "address": "fgsregerge",
            "long": "-84.125147",
            "lat": "33.9448"
        }
    ]
}

然后,你可以这样访问它:

Then, you can access it as such:

data.stores [0] data.stores [1]

这篇关于在javascript中读取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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