M
M
Maxim Timofeev2015-08-13 23:06:27
JavaScript
Maxim Timofeev, 2015-08-13 23:06:27

How to get data from json object in jquery?

there is an object

{readyState: 4, responseText: "{"companies-phone":["Phone must be an integer."]}", responseJSON: Object, status: 200, statusText: "OK"}


Need to get - Phone must be an integer.

In this case, the name companies-phone is unknown, but it is always the only one in the responseText.

Already smoke from the ears. I've tried everything, like this:
for (var key in textStatus.responseJSON) {
            var val = textStatus.responseJSON[key];
        }

I get ["Phone must be an integer."], but how do I get rid of those brackets and quotes?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2015-08-13
@webinar

{"companies-phone":["Phone must be an integer."]}
So the field itself is in the array, which is why the array is obtained.
Something like this:

for (var key in textStatus.responseJSON) {
            var val = textStatus.responseJSON[key][0];
        }

I
Ivan, 2015-08-13
@LiguidCool

Actually square brackets why in JSON?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question