javascript - Get text value of JSON key - Stack Overflow

I have this json data, stored in a jquery variable "jsondata":var jsondata = {"Name"

I have this json data, stored in a jquery variable "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.

I've tried alert(jsondata.Another[1]);, but that is, understandably, undefined. I've tried 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?

I have this json data, stored in a jquery variable "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.

I've tried alert(jsondata.Another[1]);, but that is, understandably, undefined. I've tried 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?

Share asked Aug 25, 2015 at 21:19 JakenJaken 1,3434 gold badges24 silver badges47 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

No need to use jQuery. Use 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]);

You can use Object.keys to get the keys of the object as an array:

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

This actually isn't JSON, it's an object. The variable you are looking for needs to be accessed by the key, not 1:

alert(jsondata.Another.Test2);

As a side note...if you wanted to manipulate JSON, you would use JSON.parse().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744862156a4597768.html

相关推荐

  • javascript - Get text value of JSON key - Stack Overflow

    I have this json data, stored in a jquery variable "jsondata":var jsondata = {"Name"

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信