admin管理员组

文章数量:1415139

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.

本文标签: javascriptJSON keys with dollar signStack Overflow