Jennifer
Jennifer

Reputation: 1852

Unable to parse JSON data properly in JQuery

This is my first time to convert JSON data with jQuery however, I get the following error when I try to convert the JSON string. Am I doing something wrong? I was searching for some simple sites about converting JSON string in jQuery, but most of the tutorial seems a bit difficult to understand. I would love to here some tips about converting jsons from url with jQuery if possible.

var obj = jQuery.parseJSON( '{"result":[{"id":"25","name":"loplo\n","score":"1198"},{"id":"58","name":"adjm","score":"1131"},{"id":"60","name":"dragon with ","score":"1083"},{"id":"57","name":"tDj","score":"799"},{"id":"59","name":"dragon with ","score":"452"},{"id":"55","name":"Donny","score":"450"},{"id":"56","name":"ajo ","score":"401"},{"id":"61","name":"make ","score":"392"}]}' );

Error message

Uncaught SyntaxError: Unexpected token 
 in JSON at position 35
    at JSON.parse (<anonymous>)
    at Function.m.parseJSON (jquery-1.11.1.min.js:4)
    at leaderboard.js:16

Upvotes: 0

Views: 1967

Answers (3)

Ulrich Dohou
Ulrich Dohou

Reputation: 1559

use this...

var e = '{"result":[{"id":"1351","identite":"RES ADDS","etat":"1","email":"[email protected]","telephone":"8787878","adresse":"COTONOU","nom_pays":"Togo","indicatif":"228"},{"id":"1350","identite":"MY DREAM","etat":"2","email":"[email protected]","telephone":"5248525","adresse":"COTONOU","nom_pays":"Togo","indicatif":"228"},{"id":"1349","identite":"UN ","etat":"1","email":"[email protected]","telephone":"66353364","adresse":"","nom_pays":"Benin","indicatif":"229"},{"id":"1348","identite":"DOHOU ULRICH SEMASSA &amp; Fils","etat":"1","email":"[email protected]","telephone":"66353364","adresse":"COTONOU","nom_pays":"Benin","indicatif":"229"},{"id":"1344","identite":"DC","etat":"2","email":"[email protected]","telephone":"66353364","adresse":"","nom_pays":"Afghanistan","indicatif":"93"},{"id":"1343","identite":"AGAIN","etat":"2","email":"[email protected]","telephone":"66353364","adresse":"","nom_pays":"Afghanistan","indicatif":"93"},{"id":"1342","identite":"SOCIAL LIMITED","etat":"3","email":"[email protected]","telephone":"66353364","adresse":"","nom_pays":"Afghanistan","indicatif":"93"}]}';
var json = JSON.stringify(eval('(' + e + ')'));
var arr = $.parseJSON(json);
var resultData  = arr['result'] ;

console.log(resultData) ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I changed the Json String data because yours was not working. I think it is not well formated

Upvotes: 0

Avitus
Avitus

Reputation: 15958

The reason you're getting that error message is the \n in the first element that has the name: "loplo\n"

This is because new lines are allowed in JSON objects like that as they need to be either removed or escaped.

To get around this you can do

var obj = jQuery.parseJSON(mystring.replace(/\n/g,"\\n"));

You can read more about this here:https://bugs.chromium.org/p/v8/issues/detail?id=616

Upvotes: 4

Waqas
Waqas

Reputation: 171

This is because you have the new line character as the value of loplo:

"loplo\n" Remove the \n and it should work.

I don't recommend removing all of the \n with replace because you may want that. If you want to keep the \n, add a escape character before it, like this:

"loplo\\n" and it should work.

Upvotes: 0

Related Questions