Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

javascript - Javascript如何解析JSON数组(Javascript how to parse JSON array)

I'm using Sencha Touch (ExtJS) to get a JSON message from the server.(我正在使用Sencha Touch(ExtJS)从服务器获取JSON消息。)

The message I receive is this one :(我收到的消息是这样的:)
{
"success": true,
"counters": [
    {
        "counter_name": "dsd",
        "counter_type": "sds",
        "counter_unit": "sds"
    },
    {
        "counter_name": "gdg",
        "counter_type": "dfd",
        "counter_unit": "ds"
    },
    {
        "counter_name": "sdsData",
        "counter_type": "sds",
        "counter_unit": "   dd       "
    },
    {
        "counter_name": "Stoc final",
        "counter_type": "number    ",
        "counter_unit": "litri     "
    },
    {
        "counter_name": "Consum GPL",
        "counter_type": "number    ",
        "counter_unit": "litri     "
    },
    {
        "counter_name": "sdg",
        "counter_type": "dfg",
        "counter_unit": "gfgd"
    },
    {
        "counter_name": "dfgd",
        "counter_type": "fgf",
        "counter_unit": "liggtggggri     "
    },
    {
        "counter_name": "fgd",
        "counter_type": "dfg",
        "counter_unit": "kwfgf       "
    },
    {
        "counter_name": "dfg",
        "counter_type": "dfg",
        "counter_unit": "dg"
    },
    {
        "counter_name": "gd",
        "counter_type": "dfg",
        "counter_unit": "dfg"
    }

    ]
}

My problem is that I can't parse this JSON object so that i can use each of the counter objects.(我的问题是我无法解析这个JSON对象,以便我可以使用每个计数器对象。)

I'm trying to acomplish that like this :(我试图这样做:)

var jsonData = Ext.util.JSON.decode(myMessage);
for (var counter in jsonData.counters) {
     console.log(counter.counter_name);
 }

What am i doing wrong ?(我究竟做错了什么 ?)

Thank you!(谢谢!)   ask by maephisto translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Javascript has a built in JSON parse for strings, which I think is what you have:(Javascript有一个内置的字符串JSON解析,我认为你有:)

var myObject = JSON.parse("my json string");

to use this with your example would be:(在你的例子中使用它将是:)

var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
    var counter = jsonData.counters[i];
    console.log(counter.counter_name);
}

Here is a working example(这是一个有效的例子)

EDIT : There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot).(编辑 :你使用for循环有一个错误(我在第一次阅读时错过了这个,因为当场的@Evert归功于此)。)

using a for-in loop will set the var to be the property name of the current loop, not the actual data.(使用for-in循环将var设置为当前循环的属性名称,而不是实际数据。) See my updated loop above for correct usage(有关正确用法,请参阅上面的更新循环)

IMPORTANT : the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem!(重要提示JSON.parse方法不适用于旧的旧浏览器 - 因此,如果您计划通过某种时间弯曲互联网连接使您的网站可用,这可能是一个问题!)

If you really are interested though, here is a support chart (which ticks all my boxes).(如果你真的感兴趣, 这里是一个支持图表 (勾选我的所有方框)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...