-3

I have data in this format:

[{"data":{"maximum_seconds":"-1","emergency_address_code":"","e999_code":""},{"data":{"maximum_seconds":"-1","emergency_address_code":"","e999_code":""}}]

I am trying to read using JQuery.

I have tried multiple options, but none seem to be working.

for(var i in data) {
    console.log( data.maximum_seconds);
    console.log( data[i].maximum_seconds);
}

console.log( x[1].maximum_seconds);

my code is as follows:

var x = voip_get_numbers('', ''); var x = voip_get_numbers('', ''); x.done(function(data) { $('#d').append(data);

for(var i in jsonObjData) {
    console.log( data[i] );
}

});

any my JQuery function is:

function voip_get_numbers(account_seq, customer) {
    return $.ajax({
        url : '/section/voip/numbers',
        type: "GET",
        data: {
            "action": "list_numbers",
            "seq": account_seq,
            "customer": customer,
        },
        datatype: 'json',
    });
}

I did try some others, but my code has been overwritten so i don't have it - i think its a fairly simple task but i'm not the best with JQuery and JS so excuse my ignorance.

Any help would be great, please.

Thank you

4
  • 1
    There is no jQuery in this code. Commented Mar 15, 2019 at 8:52
  • 1
    your JSON is invalid Commented Mar 15, 2019 at 8:56
  • 1
    Yep, invalid json- Commented Mar 15, 2019 at 8:56
  • Possible duplicate of As you can read this JSON using jquery or javaScript Commented Mar 15, 2019 at 8:57

3 Answers 3

1

No jQuery evident as already specified by CertainPerformance.

However, your JSON isn't properly structured. Should look like this.

 [
        {
            "data": {
                "maximum_seconds": "-1",
                "emergency_address_code": "",
                "e999_code": ""
            }
        },
        {
            "data": {
                "maximum_seconds": "-1",
                "emergency_address_code": "",
                "e999_code": ""
            }
        }
    ]

Example:

var a = [{
    "data": {
        "maximum_seconds": "-1",
        "emergency_address_code": "",
        "e999_code": ""
    }
}, {
    "data": {
        "maximum_seconds": "-1",
        "emergency_address_code": "",
        "e999_code": ""
    }
}]

//Syntax 1
for (var i in a) {
    console.log(a[i].data.maximum_seconds);
    console.log(a[i].data.emergency_address_code);
    console.log(a[i].data.e999_code);
}

//Syntax 2
for (var i = 0; i < a.length; i++) {
    console.log(a[i].data.maximum_seconds);
    console.log(a[i].data.emergency_address_code);
    console.log(a[i].data.e999_code);
}

//Syntax 3
a.forEach( x => {
    console.log(x.data.maximum_seconds);
    console.log(x.data.emergency_address_code);
    console.log(x.data.e999_code); 
} );

And then why are you repeating the "data" property. You could do this:

 [
        {
            "maximum_seconds": "-1",
            "emergency_address_code": "",
            "e999_code": ""
        },
        {
            "maximum_seconds": "-1",
            "emergency_address_code": "",
            "e999_code": ""
        }
    ]
Sign up to request clarification or add additional context in comments.

Comments

0

Your JSON file is invalid. Change your file like this:

 [{
    "data": {
        "maximum_seconds": "-1",
        "emergency_address_code": "",
        "e999_code": ""
    }
}, {

    "data": {
        "maximum_seconds": "-1",
        "emergency_address_code": "",
        "e999_code": ""
    }
}]

I used JSONLint to validate the file.

than you can parse the string to an object with

var json = JSON.parse(jsonString)

to get data from the object, use

for (var j in json) {
    var maximum_seconds = j.data.maximum_seconds
    var emergency_address_code = j.data. emergency_address_code
    var e999_code = j.data. e999_code
    // ... your code
}

1 Comment

i am using json_encode($ret); in PHP to create it. the I have a function that returns an $.ajax call
0

You need to make sure that the JSON data is JSON object and not a string. If your data is a string, then convert it to JSON object.

jQuery provides this method: $.parseJSON( jsonString )

Example:

var data = '[{"data":{"maximum_seconds":"-1","emergency_address_code":"","e999_code":""},{"data":{"maximum_seconds":"-1","emergency_address_code":"","e999_code":""}}]';

var jsonObjData = $.parseJSON( data );

2 Comments

i've add all my code to my post - if i parseJSON, how would I read the data?
since your outer data is array, so you could probably read this with alert(jsonObjData[0].data.mazimum_sources) sorry i'm outside now so i can't test it.. i'll post working example later as soon as i back home.

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.