开发者

JSON - Access field named '*' asterisk

开发者 https://www.devze.com 2023-04-09 07:28 出处:网络
I am trying to access a JSON field that has the key \'*\': { "parse": { "text": { "*": "text i want to access"

I am trying to access a JSON field that has the key '*':

{ 
  "parse": { 
    "text": {
      "*": "text i want to access" 
    }
  }
}

Neither myObject.parse.text.* nor myObject.parse.text[0] works.

I have searched for an hour but haven't found any hint that an asterisk has special 开发者_StackOverflow中文版meaning. If I just traverse the complete tree and make String comparison with if (key == "*") I can retrieve the text, but I would like to access this field directly. How can I do that?


json.parse.text["*"]

Yucky name for an object member.


Asterisks have no special meaning; it's a string like any other.

myObject.parse.text.* doesn't work because * isn't a legal JS identifier. Dot notation requires legal identifiers for each segment.

myObject.parse.text[0] doesn't work because [n] accesses the element keyed by n or an array element at index n. There is no array in the JSON, and there is nothing with a 0 key.

There is an element at the key '*', so json.parse.text['*'] works.


Try use the index operator on parse.text:

var value = object.parse.text["*"];


try to use

var text = myObject.parse.text['*']


You could do:

var json = {"parse":
 {"text":
  {"*":"text i want to access"}
 }
}

alert(json.parse.text['*']);
0

精彩评论

暂无评论...
验证码 换一张
取 消