Javascript:在JSON中查找键及其值 [英] Javascript: Find key and its value in JSON

查看:874
本文介绍了Javascript:在JSON中查找键及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以不同方式返回的JSON对象,但始终具有key.我怎么能得到它?

I have a JSON object that is returned in different ways, but always has key. How can I get it?

例如

"Records": {
    "key": "112"
}

"Records": { 
    "test": {
        "key": "512"
    }
}

甚至在数组中:

"Records": { 
    "test": {
        "test2": [
            {
                "key": "334"
            }
        ]
    }
}

尝试了几种选择,但仍然无法弄清楚(

Tried several options, but still can't figure out (

推荐答案

我不会为您编写代码,但给您一个想法可能会有所帮助,首先使用

I will not write the code for you but give you an idea may be it will help, First convert JSON object in to string using

JSON.stringify(obj);

之后,使用indexOf()方法搜索Key. 提取前一个'{'和Next'}'字符串,然后再次强制转换为JSON对象. 使用

after that search for Key using indexOf() method. Extract previous '{' and Next '}' string and again cast in to JSON object. using

var obj =  JSON.parse(string);

然后

 var value = obj.key

这篇关于Javascript:在JSON中查找键及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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