匹配两个JSON数据

时间:2015-08-21 11:54:36

标签: javascript jquery json

我有两个嵌套的json文件:

$.getJSON('data/user.json', function(data) {
  $.getJSON('data/food.json', function(data) {
  ...
  });
});

这是user.json的一个示例:

[
    {
        "userDetails": {
            "id": 27080
        },
        "Items": [
            {
                "ItemDetails": {
                    "idProduct": 1420
                },
                "quantity": 1,
                "name": "test1"
            },
            {
                "ItemDetails": {
                    "idProduct": 1959
                },
                "quantity": 5,
                "name": "test2"
            }
        ]
    }
]

这是food.json的样本:

[
    {
        "id": 1420,
        "name": "test1",
        "price": 12.5,
        "tags": [
            "cold",
            "ice"
        ],
        "proteins": 50,
        "ch": 60
    }
]

问题是: 我需要匹配user.json的idProduct和food.json的id以及打印数据。 (idProduct可以多次出现)

2 个答案:

答案 0 :(得分:0)

我首先要做的是获取两个json文件并将它们保存在本地。

   $.getJSON('data/user.json', function(data) { ... => into user
   $.getJSON('data/food.json', function(data) { ... => into food

现在,假设用户和食物都有有效的JSON对象。我会做一个小函数来检索/打印给定ID的食物对象。

我将使用一个小的缓存对象,以防文件很大,如果你之前已经提取过该项目,则不想进行完整的循环。

注意:我在这里使用underscore.js冷杉简化。

var food_cache = {};

function print_or_retreive_food(id) {

    var food_item;

    if (_.has(food_cache, id)) {
        food_item = food_cache.id;
    } else {
        _.each(food, function(item) {
            if (item.id == id) {
                food_cache[id] = item;
                food_item = item;
            }
        });
    }

    // alternatively if it is printing then do a console.log or whatever you like
    return food_item;

}

现在,您需要做的是循环用户对象并调用函数。

_.each(user, function(userItem){
    if (_.has(userItem, "Items")) {
        _.each(userItem.Items, function(item){
            print_or_retreive_food(item.ItemDetails.idProduct);
        });
    }
});

全部放在一起:



var user = [
    {
        "userDetails": {
            "id": 27080
        },
        "Items": [
            {
                "ItemDetails": {
                    "idProduct": 1420
                },
                "quantity": 1,
                "name": "test1"
            },
            {
                "ItemDetails": {
                    "idProduct": 1959
                },
                "quantity": 5,
                "name": "test2"
            }
        ]
    }
]

var food = [
    {
        "id": 1420,
        "name": "test1",
        "price": 12.5,
        "tags": [
            "cold",
            "ice"
        ],
        "proteins": 50,
        "ch": 60
    }
]

var food_cache = {};

function print_or_retreive_food(id) {

    var food_item;

    if (_.has(food_cache, id)) {
        food_item = food_cache.id;
    } else {
        _.each(food, function(item) {
            if (item.id == id) {
                food_cache[id] = item;
                food_item = item;
            }
        });
    }

    // alternatively if it is printing then do a console.log or whatever you like
   $('body').append('<p>' + JSON.stringify(food_item) + '</p>');

}

_.each(user, function(userItem){
    if (_.has(userItem, "Items")) {
        _.each(userItem.Items, function(item){
            print_or_retreive_food(item.ItemDetails.idProduct);
        });
    }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var foodList = [
    {

        "userDetails": 

    {

        "id": 27080

    },
    "Items": [

    {

        "ItemDetails": 

        {
            "idProduct": 1420
        },
        "quantity": 1,
        "name": "test1"

    },
    {

        "ItemDetails": 

                {
                    "idProduct": 1959
                },
                "quantity": 5,
                "name": "test2"
            }
        ]

    }
];
var user = [ {
        "id": 1420,
        "name": "test1",
        "price": 12.50,
        "tags": ["cold", "ice"],
        "proteins": 50,
        "ch": 60
    }];

和您的匹配代码

 var productId = user[0].id;
    for(var i=0; i < foodList[0].Items.length; i++)
    {
       var tempList = foodList[0].Items[i];
        if(tempList.ItemDetails.idProduct == productId)
          console.log("Matched");
        else
             console.log("not matched");
    }