0
{"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":"Bitte geben Sie eine gültige Zahl:"},{"DE":"Bitte geben Sie eine gültige Gewicht:"}],"FR":[{"FR":"S'il vous plaît entrer un nombre valide:"},{"FR":"S'il vous pla&#238t entrer un poids valide:"}],"PL":[{"PL":"Proszę wpisać aktualny numer:"},{"PL":"Proszę podać poprawny waga:"}]}

I want the data "Please enter a valid weight:"

I tried alert(json['EN'][1]); //it gives me Object object alert

Tadeck
  • 132,510
  • 28
  • 152
  • 198
coolguy
  • 7,866
  • 9
  • 45
  • 71
  • That's the correct output as `json['EN'][1]` is an object. Obviously you know how to access object properties, so the last step should follow easily. – Felix Kling Mar 16 '12 at 03:44

2 Answers2

2

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?

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
1

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 number

alert(json.NL[0].NL);
// Vul een geldig nummer: in
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390