1

I am facing problem with json parse. I have this in php:

 json_encode(getTeams(),JSON_HEX_APOS);

it return huge data.

Sample data: for more clarification lets say i have this:

my_encoded_data = 
     {
        "13": "Germany-1",
        "14": "Russia-1",
        "15": "Switzerland-1",
        "16": "Canada-1",
        "17": "USA-1",
        "18": "USA-2",
        "19": "Germany-2",
        "20": "Italy-1",
        "21": "Switzerland-2",
        "22": "Austria-1",
        "23": "Italy-2",
        "24": "Netherlands-1",
        "25": "Poland-1",
        "26": "Latvia-1",
        "27": "Russia-2",
        "28": "Czech Republic-1",
        "29": "Great Britain-1",
        "30": "France-1",
        "31": "Canada-2",
        "32": "Slovakia-1",
        "43": "A. Florschütz/T. Wustlich",
        "44": "P. Leitner/A. Resch",
        "46": "G. Albrecht/E. Pothier",
        "48": "C. Moffat/M. Moffat",
        "50": "V. Boizov/D. Khamkin",
        "51": "M. Kuzmitch/J. Veselov",
        "53": "T. Schiegl/M. Schiegl",
        "56": "P. Griffal/D. Joye",
        "59": "A. Linger/W. Linger",
        "62": "G. Plankensteiner/O. Haselrieder",
        "65": "A. Sics/J. Sics",
        "68": "C. Oberstolz/P. Gruber"
    }

I have tested this data by spilt it into small parts to validate in jsonlint. it shows me valid json. then i have assigned it in javascript in:

window.objteamsFromServer  = my_encoded_data;

Then i wanted to parse it in json like this:

arrSearch = window.objteamsFromServer;

It gives me this error:

JSON.parse: bad escaped character 

how can i solve this?

let me know if any informations needed .

Thanks,
Awlad

1
  • Where does my_encoded_data come from? What does the PHP code that generates the JavaScript code look like? (You seem to be using PHP to generate JavaScript.) What does your call to JSON.parse look like? Commented Jan 28, 2014 at 11:54

2 Answers 2

1

It's very hard to tell from your question exactly what my_encoded_data is, but it sounds as though you're outputting the result of json_encode into JavaScript source code, e.g. (from the browser's perspective):

window.objteamsFromServer = {
    "13": "Germany-1",
    "14": "Russia-1",
    "15": "Switzerland-1",
    "16": "Canada-1",
    "17": "USA-1",
    // ...and so on
};

and then also trying to parse it with JSON.parse.

You wouldn't do that. The JavaScript engine will have already parsed that object initializer (it's not JSON, it's JavaScript source code), so you would just use the value directly:

console.log(window.objteamsFromServer[13]); // "Germany-1"
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you might be getting this error because of the following line:

"43": "A. Florschütz/T. Wustlich",

Notice the 'ü' in the name above.

One approach is to encode the data at server side and then decode it on client side. Use JavaScripts decodeURIComponent function do decode at client side.

For server-side encoding of data, please refer php documentation.

1 Comment

There's no problem with the ü character in JavaScript (or JSON), provided the text is being read by the browser in the same encoding in which it was sent by the server.

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.