Skip to content Skip to sidebar Skip to footer

How Do I Access The Contents In This Json?

{'EN':[{'EN':'please enter a valid number :'},{'EN':'Please enter a valid weight:'}],'NL':[{'NL':'Vul een geldig nummer: in'},{'NL':'Vul een geldig gewicht: in'}],'DE':[{'DE':'Bitt

Solution 1:

Solution

If your object is called json, then the solution is the following:

json['EN'][1]['EN']

Explanation

By json['EN'][1] you are getting the following object:

{"EN":"Please enter a valid weight:"}

so the only thing left is to access value associated to its "EN" key.

Ps. Of course you can access properties in JavaScript in two ways, by eg. json['EN'] or json.EN, but the first one is preferred. Square bracket notation is treated as best practice, it is more flexible. More on this subject: JavaScript property access: dot notation vs. brackets?

Solution 2:

You were nearly there. The outer EN is an array of object literals, and the inner EN is an object property. You are looking for the second array element ([1]) of the outer property EN:

Normal JavaScript object syntax, using dotted properties and bracketed array indices:

alert(json.EN[1].EN);
// please enter a valid weight

Alternate syntax using bracketed object properties:

alert(json["EN"][0]["EN"]);


// Others...alert(json.EN[0].EN);
// please enter a valid numberalert(json.NL[0].NL);
// Vul een geldig nummer: in

Post a Comment for "How Do I Access The Contents In This Json?"