Tags
JSON , parse
Asked 6 years ago
10 Jan 2018
Views 1179
jaggy

jaggy posted

JSON.parse object value not accessible


var data ='{"assigned-to":"4","mname":"jaggy","green":0}';
 var d=JSON.parse(data);
alert(d.mname);
alert(d.assigned-to);


json have used "-" at key name so when i call with object JSON . it cant accessible
rajiv

rajiv
answered Apr 25 '23 00:00

When you parse a JSON string using JavaScript's JSON.parse() method, the resulting object should contain all of the properties and values that were defined in the JSON string. To access the value of a property in the parsed JSON object, you can use either dot notation or bracket notation.

For example:



// Define a JSON string
var jsonString = '{"name": "John", "age": 30, "city": "New York"}';

// Parse the JSON string into an object
var jsonObj = JSON.parse(jsonString);

// Access the value of a property using dot notation
console.log(jsonObj.name); // Output: John

// Access the value of a property using bracket notation
console.log(jsonObj["age"]); // Output: 30

If you are having trouble accessing the value of a property in the parsed JSON object, it may be due to an issue with the JSON string itself. It's a good idea to use a tool like JSONLint (https://jsonlint.com/) to validate the syntax of your JSON string and make sure it is properly formatted.

Additionally, it's important to ensure that the parsed JSON object is being assigned to a variable correctly and that the variable is in the correct scope for accessing the object's properties
Post Answer