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

javascript - JSON keys with dollar sign - Stack Overflow

programmeradmin0浏览0评论

I am trying to parse some json using js and angularjs, which it's path is: item["sub:item"].$.url.

Everything goes fine, until the $ sign. I have tried to do ng-repeat with on item["sub:item"].["$"].url and item["sub:item"].$.url but nothing works.

Is there any solution?

I am trying to parse some json using js and angularjs, which it's path is: item["sub:item"].$.url.

Everything goes fine, until the $ sign. I have tried to do ng-repeat with on item["sub:item"].["$"].url and item["sub:item"].$.url but nothing works.

Is there any solution?

Share Improve this question edited Oct 31, 2015 at 17:51 Ronen Ka asked Oct 31, 2015 at 17:30 Ronen KaRonen Ka 312 silver badges8 bronze badges 2
  • 5 item.subitem["$"].url – Artless Commented Oct 31, 2015 at 17:31
  • 4 $ and _ are valid property/variable name characters. They can even be used as first character. I have a feeling something else is wrong here. – Joseph Commented Oct 31, 2015 at 17:34
Add a ment  | 

2 Answers 2

Reset to default 2

@Ronen, I have tried both methods and they are working fine. There must be some other issue.

Example

Kindly check console for output.

(function init(){
    var data = {
        '$':{
        	url: window.location.href,
            site: 'stackoverflow'
        },
        'name':'Hello World'
    }
    
	console.log(data['$'].url, data['$'].site);
        console.log(data.$.url, data.$.site);
})()

Updated Snippet

(function init(){
    var item = {
        'sub:item':{
        	"$":{
            	url: window.location.href,
            	site: 'stackoverflow'
            } 
        }
    }
    
	console.log(item["sub:item"].$.url, item["sub:item"].$.site);
})()

a = {'subitem' : {'$' : "I'm  here."}};
a['subitem'].$ //prints "I'm here." in the console.

Your flaw is claiming that the given: "The path is item["sub:item"].$.url"

You need to prove this ^ by providing the full data structure you're trying to access.

In chrome you can print out the data structure with just "ctrl+shift+j" and type the variable name and press enter.

If the path were actually as you have it written in your question, you would have answered your own question already.

发布评论

评论列表(0)

  1. 暂无评论