0

Jsonlint display that this JSON object is valid:

 [{"obj":{"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]","path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"}}] 

I'm trying to access to markers with the k, B and path elements but it's always set to undefined. Here is my code:

 try {

       var jsonData = JSON.parse(myJson);
       console.log(jsonData.obj[0].markers[0].k);
     }
 catch (e) {
             console.error("Parsing error:", e);
           }

Can someone tell me how to access to the element of my JSON object properly? Thanks for the help.

5
  • obj is an object, not an array Commented Dec 11, 2014 at 0:02
  • @chazsolo No, obj is just an index of an array that I used in my MySQL query like that: $rows[] = array('obj' => $r);echo json_encode($rows); It can be 'Bob' ... Commented Dec 11, 2014 at 0:04
  • Its because markers object is a string not an array so the value of "markers": "value here in string" Commented Dec 11, 2014 at 0:07
  • @loanburger What do you mean, the array is encoded into a JSON object anyway with json_encode. JSON.parse() JavaScript function works well when I get back my JSON object. The type os this JSON description is [object Object] after a JSON.parse(). Can you explain with an example please ? Commented Dec 11, 2014 at 0:09
  • You escaped k: \"k\" is not an index - it's a value of the markers index. Commented Dec 11, 2014 at 0:14

2 Answers 2

2

Something must have gone wrong in creating this string. Yes, it's valid JSON, but it has a different format than you think, because you escape control characters like " and [, ].

Try this string instead:

[
   {
      "obj":{
         "markers":[
            {
               "k":47.040182144806664,
               "B":0.52734375
            },
            {
               "k":50.90303283111257,
               "B":10.37109375
            },
            {
               "k":52.53627304145945,
               "B":-1.7578125
            },
            {
               "k":41.77131167976406,
               "B":-6.591796875
            }
         ],
         "path":[
            [
               47.040182144806664,
               0.52734375
            ],
            [
               50.90303283111257,
               10.37109375
            ],
            [
               52.53627304145945,
               -1.7578125
            ],
            [
               41.77131167976406,
               -6.591796875
            ]
         ]
      }
   }
]

as opposed to your string:

[
   {
      "obj":{
         "markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
         "path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"
      }
   }
]
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe your JSON description is more well formed, I just tested it but still undefined when I'm trying to access to the element after JSON.parse().
How did you access the value? To obtain the first 47.040182144806664, I do jsonData[0].obj.markers[0].k. Did you do the same? This works fine for me...
Sorry your version is right, now it's better, thank you!
0

You have two issues from what I can tell:

One, yes it is valid Json but the marker and path object values are enclosed in string quotes:

"markers":"[{\"k\":47.040182144806664,\"B\":0.52734375},{\"k\":50.90303283111257,\"B\":10.37109375},{\"k\":52.53627304145945,\"B\":-1.7578125},{\"k\":41.77131167976406,\"B\":-6.591796875}]",
"path":"[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]"

what you rather want is:

"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],

"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]

But given the above, also not escape the k and b object names for markers \"k\" should be "k":

So the completed edited JSON would look like this:

[{"obj":{"markers":[{"k":47.040182144806664,"B":0.52734375},{"k":50.90303283111257,"B":10.37109375},{"k":52.53627304145945,"B":-1.7578125},{"k":41.77131167976406,"B":-6.591796875}],"path":[[47.040182144806664,0.52734375],[50.90303283111257,10.37109375],[52.53627304145945,-1.7578125],[41.77131167976406,-6.591796875]]}}]

2 Comments

You're markers and path still double quoted in your answer ?
Yes that's just the object names but the values are not in quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.