简体   繁体   English

获取JSON密钥的文本值

[英]Get text value of JSON key

I have this json data, stored in a jquery variable " jsondata ": 我有此json数据,存储在jquery变量“ jsondata ”中:

var jsondata = {
  "Name": {
    "Jaken": {},
    "Test": {},
    "Hello": {}
  },
  "Date": {
    "Today": {},
    "Tomorrow": {},
    "Wednesday": {}
  },
  "Description": {
    "Me": {},
    "Tester": {},
    "World": {}
  },
    "Another": {
      "Test": {},
      "Test2": {}
  }
};

I'm trying to find out how to get the word "Test" as a string. 我试图找出如何将单词“ Test”作为字符串。

I've tried alert(jsondata.Another[1]); 我试过alert(jsondata.Another[1]); , but that is, understandably, undefined . ,但可以理解的是,它是undefined I've tried alert(value.jsondata.Another[1]); 我已经尝试过alert(value.jsondata.Another[1]); (Not sure why I thought this would work, but it was worth a shot I guess. (不知道为什么我认为这行得通,但是我猜值得一试。

Is there any documentation that shows how to find the key name of json data in jquery as a string? 是否有任何文档显示如何在jquery中以字符串形式查找json数据的键名?

No need to use jQuery . 无需使用jQuery Use Object.keys 使用Object.keys

 var jsondata = { "Name": { "Jaken": {}, "Test": {}, "Hello": {} }, "Date": { "Today": {}, "Tomorrow": {}, "Wednesday": {} }, "Description": { "Me": {}, "Tester": {}, "World": {} }, "Another": { "Test": {}, "Test2": {} } }; console.log(Object.keys(jsondata.Another)[0]); 

您可以使用Object.keys来获取对象的键作为数组:

Object.keys(jsondata.Another)[0];

This actually isn't JSON , it's an object . 这实际上不是JSON ,而是一个对象 The variable you are looking for needs to be accessed by the key, not 1 : 您要查找的变量需要通过键而不是1来访问:

alert(jsondata.Another.Test2);

As a side note...if you wanted to manipulate JSON, you would use JSON.parse() . 附带说明...如果您想操作JSON,则可以使用JSON.parse()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM