I transfer data from the server. I get the date on the client.

Parsu:

var msg = JSON.parse(data); console.log(msg); 

I receive:

 {'id': 17, 'created_at': None, 'modified_at': None, 'first_name': 'ccc', 'last_name': 'zzzzzzzzzxxxxxxxxx', 'full_name': 'xxxxxxxxxccc', 'email': None, 'password': None, 'active': True, 'confirmed_at': None, 'last_login_at': None, 'current_login_at': None, 'last_login_ip': None, 'current_login_ip': None, 'login_count': None, 'created_by': None} 

But when I try to get the value by key, msg.id or msg["id"] , I get undefined in response

I assume that the matter is “validity”, or None is available.

Tell me what to do? It is desirable that the data do not have to be changed on the server side.

Thanks in advance, and I apologize for the banal question.

  • console.log(data); ? and what are these None , True and single quotes? - Igor
  • console.log (data) returns "{'id': 17, 'created_at': None, 'modified_at': None, 'first_name': 'cccasd', 'last_name': 'zzzzzzzzzxxxxxxxxxx', 'full_name': 'xxxxxxxxxccc' , 'email': None, 'password': None, 'active': True, 'confirmed_at': None, 'last_login_at': None, 'current_login_at': None, 'last_login_ip': None, 'current_login_ip': None, ' login_count ': None,' created_by ': None} " - Narnik Gamarnik
  • None and True - such data comes from the backend - Narnik Gamarnik
  • I am sure that single quotes do not matter. - Narnik Gamarnik
  • which outputs: console.log(typeof msg); - Grundy

3 answers 3

 var newData = data.replace(/\'/g, "\""); // Меняем кавычки newData = newData.replace(/\None/g, "null"); // Меняем None на null newData = newData.replace(/\True/g, "true"); // Меняем True на trur newData = newData.substr(1, newData.length-2); // Обрезаем первые и последние кавычки var msg = JSON.parse(newData); console.log(msg); 
  • SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 4 of the JSON - Narnik Gamarnik
  • The string was: "{" id ": 17," created_at ": null," modified_at ": null," first_name ":" 12356 "," last_name ":" 121212zzzzzzasdasdasd "," full_name ":" 1 "," email ": null," password ": null," active ": false," confirmed_at ": null," last_login_at ": null," current_login_at ": null," last_login_ip ": null," current_login_ip ": null," login_count ": null, "created_by": null} " - Narnik Gamarnik
  • Which quotation marks at the beginning and at the end is part of the text? - grnch
  • This is the string after the replacement and before the conversion. - Narnik Gamarnik
  • one
    Add a description of your solution to your answer to make it easier for users to understand your answer. - Yuri

There are a few basic rules for creating a JSON string:

  1. The JSON string contains either an array of values ​​or an object (an associative array of name / value pairs).
  2. The array is enclosed in square brackets ([and]) and contains a comma-separated list of values.
  3. The object is enclosed in braces ({and}) and contains a comma-separated list of name / value pairs.
  4. A name / value pair consists of a field name enclosed in double quotes, followed by a colon (:) and the field value.
  5. The value in an array or object can be:
    • By number (whole or floating point)
    • String (in double quotes)
    • Boolean (true or false)
    • Another array (enclosed in square brackets)
    • Other object (enclosed in braces)
    • Null value

To include double quotes in a string, you must use a backslash: \ ". Just like in many programming languages, you can place control characters and hexadecimal codes in a string, preceding them with a backslash.

    Not sure if this will work, and probably not safe, but you can try this:

    var None = null; var True = true; eval ("var msg =" + data);