0

i have this Json:

"AMAZ": {
    "LOAS": {
        "1": "NAME1",
        "2": "NAME2"
    },
"BAG": {
    "ASLO": {
        "1": "NAME1",
        "2": "NAME2"
    }

im trying to get the NAME1 and NAME2 text.

I tried with:

var json = $.parseJSON($("#json").val());
var test = json.AMAZ.LOAS.'1';
alert(test);

but i get an error here:

var test = json.AMAZ.LOAS.'1';

i get Unexpected string '1'

How can i get the values?

1
  • 1
    that number is index to get index use json.AMAZ.LOAS[1] Commented Oct 24, 2019 at 0:45

1 Answer 1

1

when you json.parse, your "1"'s and "2"'s become the most inner values in your json, as well they become integers when parsed (I don't know why). you can use them in brackets:

var data = JSON.parse(`
  {"AMAZ": {"LOAS": {
        "1": "NAME1",
        "2": "NAME2"
    },
"BAG": {
    "ASLO": {
        "1": "NAME1",
        "2": "NAME2"
    }}}}`); 

// Try edit message

console.log(data)
console.log(data.AMAZ.LOAS[1])

hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

var myvariable = data.AMAZ.LOAS[1];
sorry, I just understood what you asked for. var id = 1 or var id = 2. you can also loop through the data in a for loop.
Dont worry i did it yesterday. thanks for your help!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.