-1

I have this JSON string that has keys in the form of a URL:

 "{\n  \"http:\/\/it.dbpedia.org\/resource\/Pasadena\" : {
  \"http:\/\/www.w3.org\/2002\/07\/owl#sameAs\" : [ { \"type\" : \"uri\", \"value\" : 
 \"http:\/\/dbpedia.org\/resource\/Pasadena\" } ] } ,\n 
 \"http:\/\/cs.dbpedia.org\/resource\/Pasadena_(rozcestn\\u00EDk)\" : {
 \"http:\/\/www.w3.org\/2002\/07\/owl#sameAs\" : [ { \"type\" : \"uri\", \"value\" : 
 \"http:\/\/dbpedia.org\/resource\/Pasadena\" } ] } ,\n 
 \"http:\/\/de.dbpedia.org\/resource\/Pasadena\" : { 
 \"http:\/\/www.w3.org\/2002\/07\/owl#sameAs\" : [ { \"type\" : \"uri\", \"value\" : 
\"http:\/\/dbpedia.org\/resource\/Pasadena\" } ] } ...

I need to get the array with "http:\/\/www.w3.org\/2002\/07\/owl#sameAs\" as key.

I parsed it in JS with JSON.parse(). But unable to locate this key.

    var doc = JSON.parse(req.responseText);
    var str = "http://dbpedia.org/ontology/populationTotal";
    var popTotal = doc.str;
    var population = popTotal[0].value;

But this gives undefined as expected. How do I get the values with these characters?

VishalHemnani
  • 312
  • 3
  • 13
  • Your question has very little to do with http being in the string. It's just about how to access a property using any string. – Barmar Jan 09 '14 at 09:18

1 Answers1

2

You are referencing the value incorrectly. The line

var popTotal = doc.str;

Should be

var popTotal = doc[str];

In the seconds case it will use str as the property name, in the first it will look for a property called "str".

major-mann
  • 2,602
  • 22
  • 37
  • So, I don't need to escape any characters from that URL? – VishalHemnani Jan 09 '14 at 09:29
  • No... JSON supports any character as the name. You would usually only have to escape quotes when entering the name so they can be distinguished from the end quote (And obviously this escaping would only take place when you enter these directly as text). – major-mann Jan 09 '14 at 09:31