How to print jquery object / array

I get id name and category from mysql database.

When I warn the result, I get:

[{"id":"197","category":"Damskie"},"id":"198","category":"M\u0119skie"}] 

(This object?)

  • How to print the result as follows:

    Ladies

    M \ u0119skie

  • M \ u0119ski - has a bad encoding. It should be Mฤ™skie . How can i change this?

+6
source share
3 answers
 var arrofobject = [{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}]; $.each(arrofobject, function(index, val) { console.log(val.category); }); 
+22
source

What you have on the server is a line as shown below:

 var data = '[{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}]'; 

Then you can use the JSON.parse function to change it to an object. Then you get access to the category as shown below:

 var dataObj = JSON.parse(data); console.log(dataObj[0].category); //will return Damskie console.log(dataObj[1].category); //will return Mฤ™skie 
+6
source

Your result is currently in string format, you need to parse it as json.

 var obj = $.parseJSON(result); alert(obj[0].category); 

Also, if you set the dataType type for the ajax call you make in json , you can skip the $.parseJSON() step.

+2
source

Source: https://habr.com/ru/post/913639/


All Articles