如何查询JSON对象

时间:2016-09-21 19:56:28

标签: json ionic-framework

我正在使用具有以下数据结构的json文件。

{"students":[
    {"Name":"Wale", "state":"Lagos", "age":20, "hobby":"dancing"},
    {"Name":"Ebere", "state":"Enugu", "age":18, "hobby":"eating"},
    {"Name":"Musa", "state":"Kano", "age":24, "hobby":"swimming"}
]}

我正在使用离子框架,需要从三个变量中选择所有学生说...... 国家,年龄和爱好。如果这是一个sql语句,它看起来像....

$query = "Select * from students WHERE state = 'Enugu' AND age = '20' AND hobby = 'swimming'".

所以我的问题是,我怎么能在json中做到这一点?

2 个答案:

答案 0 :(得分:0)

如果您正在寻找自己的特殊用途语言,请查看JSONiq,这是一种专为JSON数据模型设计的查询语言。以下是从Wikipedia获取的简单示例:

for $p in collection("persons")
where $p.age gt 20
let $home := $p.phoneNumber[][$$.type eq "home"].number
group by $area := substring-before($home, " ")
return 
{
     "area code" : $area,
     "count" : count($p)
} 

如果您想要类似于SQL join子句的功能,请查看下面的example

let $stores :=
[
    { "store number" : 1, "state" : "MA" },
    { "store number" : 2, "state" : "MA" },
    { "store number" : 3, "state" : "CA" },
    { "store number" : 4, "state" : "CA" }
]
let $sales := [
     { "product" : "broiler", "store number" : 1, "quantity" : 20  },
     { "product" : "toaster", "store number" : 2, "quantity" : 100 },
     { "product" : "toaster", "store number" : 2, "quantity" : 50 },
     { "product" : "toaster", "store number" : 3, "quantity" : 50 },
     { "product" : "blender", "store number" : 3, "quantity" : 100 },
     { "product" : "blender", "store number" : 3, "quantity" : 150 },
     { "product" : "socks", "store number" : 1, "quantity" : 500 },
     { "product" : "socks", "store number" : 2, "quantity" : 10 },
     { "product" : "shirt", "store number" : 3, "quantity" : 10 }
]
let $join :=
    for $store in $stores[], $sale in $sales[]
    where $store."store number" = $sale."store number"
    return {
        "nb" : $store."store number",
        "state" : $store.state,
        "sold" : $sale.product
    }
return [$join]
-> [
    { "nb" : 1, "state" : "MA", "sold" : "broiler" },
    { "nb" : 1, "state" : "MA", "sold" : "socks" },
    { "nb" : 2, "state" : "MA", "sold" : "toaster" },
    { "nb" : 2, "state" : "MA", "sold" : "toaster" },
    { "nb" : 2, "state" : "MA", "sold" : "socks" },
    { "nb" : 3, "state" : "CA", "sold" : "toaster" },
    { "nb" : 3, "state" : "CA", "sold" : "blender" },
    { "nb" : 3, "state" : "CA", "sold" : "blender" },
    { "nb" : 3, "state" : "CA", "sold" : "shirt" }
]

答案 1 :(得分:0)

您可以使用.filter()来查询""你的数据集。对于您描述的具体情况,将是:



let data = {"students":[
    {"Name":"Wale", "state":"Lagos", "age":20, "hobby":"dancing"},
    {"Name":"Ebere", "state":"Enugu", "age":18, "hobby":"eating"},
    {"Name":"Musa", "state":"Kano", "age":24, "hobby":"swimming"}
]};

// ES6!!!
function filterStudents(students, name, age, state){
    return students.filter(student => student.state === state && student.age === age && student.Name === name)
}

console.log(filterStudents(data.students, 'Musa', 24, 'Kano'))




但我建议您使用库而不是手动创建过滤案例。看看那个lib(我是创作者)是否适合你的情况:

https://github.com/DiegoZoracKy/data-query