使用JSON数据解析特定数据

时间:2019-07-20 00:51:55

标签: node.js json xml typescript discord.js

尝试根据以下XML数据的“名称”查找“年龄”。

 $args = array(

        's' => "fine here+fine t",
        'post_type' => 'post',
        'post_status' => 'publish',
    'sentence' => true,
        'paged'=>$paged,
        'meta_query' => [

            'product_price'  => [ 'key' => 'product_price', 'value' => '0', 'compare' => '>' , 'type'=>'NUMERIC'],


    ],

    'orderby' => 'product_price',
    'order' => 'ASC'
    );

我使用xml2json对此进行了解析,返回了

    <data>
        <user>
            <name>Joe</name>
            <age>34</age>
        </user>
        <user>
            <name>Jimmy</name>
            <age>26</age>
        </user>
    </data>

这样,我如何通过输入“ Joe”获得34的值?

1 个答案:

答案 0 :(得分:1)

Parse字符串,然后find记录

const json = JSON.parse("...");
const joe = json.data.user.find(x => x.name === "Joe");
console.log(joe.age);
相关问题