最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get text value of JSON key - Stack Overflow

programmeradmin4浏览0评论

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().

发布评论

评论列表(0)

  1. 暂无评论