0

I am using the json parser from json.org to handle data from my application. The problem is that the parser cannot handle some json formats.

One request receives the data below.

  <?php

  $obj = array("cities"=>array("city1","city2","city3","city4","city5"));

  echo json_encode($obj);

  ?>

Results in the json below

{
"cities": ["city1","city2","city3","city4","city5"] 
}

the code below handles the above data

  var data = json_parse(XMLHttpRequestObject.responseText, function (key, value){

  alert(key +' = '+value);

  });

The parser fails and throws an error.

Does anyone know how to handle such an object.

2
  • Please always, always include the exact error message! Commented Jul 1, 2010 at 9:07
  • 1
    You've missed something out from your edit (hint: Pekka is asking for it) Commented Jul 1, 2010 at 9:39

5 Answers 5

3

I executed the following for a quick test and it seems to work:

var text = '{ "cities": ["city1","city2","city3","city4","city5"] }';
var data = json_parse(text, function (key, value){
  document.write(key + ' = ' + value + '<br/>');
});
document.write('result = ' + data);

It recursively walks the structure and the result is this:

0 = city1
1 = city2
2 = city3
3 = city4
4 = city5
cities = ,,,,
= [object Object]
result = undefined

What is in your XMLHttpRequestObject.responseText field?

Also, aren't you supposed to return a value from your function(key, value)?

Sign up to request clarification or add additional context in comments.

Comments

1

You need to put your keys and values into double quotes:

{
"cities": ["city1","city2","city3","city4","city5"] 
}

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

You can use jsonlint to validate the code.

Comments

0

I'd wager that the problem lies in your data. The '' before city3 is wrong.

It would help if you include some information on the error thrown.

Comments

0

The parser fails because the JSON data is malformed. There are two quotes in front of city3 and the starting quote for city4 is missing.

{
cities: ['city1','city2','city3','city4','city5'] 
}

Are you in control of the code that generates this output? It looks like it's being built by hand, while if possible it should be generated using a JSON library.

PHP example:

$output = array(
  'cities' => array('city1', 'city2', 'city3', 'city4', 'city5')
);
echo json_encode($output);

Output:

{"cities":["city1","city2","city3","city4","city5"]}

1 Comment

If u handle that output with the parser as i have done: json_parse(XMLHttpRequestObject.responseText, function (key, value){ alert(key +' = '+value); }) you dont get the expected result as you would get if you used a 'for in' loop on the object. The expected key returned should be "cities", not 0, 1, 2, 3, 4
0

The problem seems to be in your application's json encoding algorithm.

Since you did't specify the application language, I cannot tell you the exact function/method to use, but I suggest you to use standard json encoding techniques instead reinventing the wheel.

For example in php you can use the json_encode standard function of one of the many encoding libraries in the open source world.

1 Comment

The code is produced by php. If u run the code you will realize that the KEYs are indexes. Expected result: alert('cities' = [object]);

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.