使用多个字符串值查找JSON嵌套节点 [英] Find JSON nested nodes using multiple string values

查看:73
本文介绍了使用多个字符串值查找JSON嵌套节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要根据两个不同参数计算价格的屏幕,主要(示例中的活动)和子项(示例中的ticketType)

I have a screen that needs to calculate prices based on two different parameters, primary (activity in example) and children (ticketType in example)

此前已完成hackishly,所以我决定最好的选择是将价格存储在运行时填充的JSON对象中,并基于点击次数和改变事件

Previously this was done hackishly, so i decided the best option would be to store the prices in a JSON object populated at runtime, and based on clicks & change events

所以我制定了这种json格式,请注意你也可以改变它,而不是一成不变

So i have this json format I formulated, please note you can change this as well, not set in stone

{
    "activities": [{
        "activitycode": "TK",
            "desc": "Tickets",
            "ticketTypes": [{
            "ticketcode": "G",
                "baseprice": 79
        }, {
            "ticketcode": "P",
                "baseprice": 109
        }]
    }, {
        "activitycode": "PK",
            "desc": "Parking",
            "ticketTypes": [{
            "ticketcode": "BK",
                "baseprice": 65
        }, {
            "ticketcode": "ESC-I",
                "baseprice": 40
        }]
    }], //end activities
    "handlingPercent": "0.03",
        "shipping": "15"
}

所以,我尝试以正确的价格遍历JSON对象,比如这个

So, I tried traverse the JSON object for the correct price, like this

pricing.activities.activitycode['TK'].ticketTypes.ticketcode['G'].baseprice

但是这不起作用,因为在提供我的值之前我还没有指定要去哪个数字节点

but this doesnt work, because I havent specified which numerical node to go to yet before supplying my values

一旦我做了以下作品

pricing.activities[0].ticketTypes[0].baseprice

但我想要完成的是提供字符串/文本值,遍历父节点,然后是子节点,并找到值

But what i would like to accomplish is supplying string/text values, traverse parent nodes, then child nodes, and find the value

我想我可以做这样的$ .each迭代,但我想知道专家们先想到了什么,如果这确实是最好的方式去做事情

I suppose i could do an $.each iteration like this, but i wanted to know what the experts thought first, if this is indeed the best way to go about things

$.each(pricing.activities, function(arrayID, activityData) {    
if(activityData.activitycode=="TK")
    $.each(activityData.ticketTypes, function(ticketTypeID, typeData) {
        if(typeData.ticketcode=="G")
        alert(typeData.baseprice);
     });
});


推荐答案

在谈论代码之前,让我们来谈谈你的JSON数据。

Before talking about the code, let's talk about your JSON data.

首先,我重新格式化数据,以便更容易地遵循结构:

First, I reformatted the data so I could follow the structure more easily:

{
    "activities": [
        {
            "activitycode": "TK",
            "desc": "Tickets",
            "ticketTypes": [
                { "ticketcode": "G", "baseprice": 79 },
                { "ticketcode": "P", "baseprice": 109 }
            ]
        },
        {
            "activitycode": "PK",
            "desc": "Parking",
            "ticketTypes": [
                { "ticketcode": "BK", "baseprice": 65 },
                { "ticketcode": "ESC-I", "baseprice": 40 }
            ]
        }
    ],
    "handlingPercent": "0.03",
    "shipping": "15"
}

既然结构清晰,我在这里问题是:你有一个活动数组,并且在该数组的每个元素中都有一个 ticketTypes 数组。

Now that the structure is clear, here is the question: You have an activities array, and inside each of the elements of that array there is a ticketTypes array.

这些数组中的项目顺序是否重要?即它们是否用于生成必须按正确顺序显示的列表?

Does the order of items in these arrays matter? i.e. are they used to generate a display list that must be in the correct order?

并且,您如何访问这些数组?你主要是通过它们循环并对所有元素做些什么吗?或者你使用它们来查找已知 activitycode ticketcode 的单个元素 - 这就是嵌套的<$问题结束时c $ c> $。每个循环结束了吗?

And, how do you need to access these arrays? Do you primarily loop through them and do something with all the elements? Or do you use them to look up individual elements given a known activitycode and ticketcode - which is what the nested $.each loops at the end of your question end up doing?

根据这些问题的答案,你可以更好地服务于不同类型的结构。也许是这样的:

Depending on the answers to these questions, you may be better served with a different kind of structure. Perhaps like this:

{
    "activities": {
        "TK": {
            "desc": "Tickets",
            "ticketTypes": {
                "G": { "baseprice": 79 },
                "P": { "baseprice": 109 }
            }
        },
        "PK": {
            "desc": "Parking",
            "ticketTypes": {
                "BK": { "baseprice": 65 },
                "ESC-I": { "baseprice": 40 }
            }
        }
    },
    "handlingPercent": "0.03",
    "shipping": "15"
}

因为现在这段代码(我也在这里清理了缩进):

Because now this code (I cleaned up the indentation here too):

$.each( pricing.activities, function( arrayID, activityData ) {
    if( activityData.activitycode=="TK" ) {
        $.each( activityData.ticketTypes, function( ticketTypeID, typeData ) {
            if( typeData.ticketcode=="G" )
                alert( typeData.baseprice );
         });
    }
});

可替换为:

alert( pricing.activities.TK.ticketTypes.G.baseprice );

以及您编写的其他类似代码也会更简单。关于你唯一丢失的方法是保证订购活动 ticketTypes :数组有保证订单,对象没有。

and other similar code that you write will also be simpler. About the only thing you lose this way is the guarantee of ordering of the activities and ticketTypes: arrays have a guaranteed order, objects do not.

这篇关于使用多个字符串值查找JSON嵌套节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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