How to convert js array of JSON object strings to array of JS objects

This tutorial explains how to json object string to js object array.

If it's JS array of JSON object strings,we use String.map method to iterate the object string,

and use JSON.parse to parse this json object string.

If it's a single JSON string received from a server.We just use JSON.parse method to convert it to array of JS objects.

<script>

var jsonArr = ['{\

       "food": "Sauce - Thousand Island",\

       "cities": "Soanindrariny",\

       "animals": "Common boubou shrike"\

   }',

'{\

       "food": "Goat - Whole Cut",\

       "cities": "Kargowa",\

       "animals": "Sheep, red"\

   }'];

// convert to js array of js objects

var objs = jsonArr.map(JSON.parse);

console.log(objs);

//0: {food: "Sauce - Thousand Island", cities: "Soanindrariny", animals: "Common boubou shrike"}

//1: {food: "Goat - Whole Cut", cities: "Kargowa", animals: "Sheep, red"}


var jsonStr = '[{\

       "food": "Sauce - Thousand Island",\

       "cities": "Soanindrariny",\

       "animals": "Common boubou shrike"\

   },\

   {\

       "food": "Goat - Whole Cut",\

       "cities": "Kargowa",\

       "animals": "Sheep, red"\

   }]';

var objs = JSON.parse(jsonStr);

console.log(objs);

//0: {food: "Sauce - Thousand Island", cities: "Soanindrariny", animals: "Common boubou shrike"}

//1: {food: "Goat - Whole Cut", cities: "Kargowa", animals: "Sheep, red"}

</script>


date2019-04-01 00:34:12 From:www.jquerypluginhub.com Author:JPH
Related Tags:
Comments

Leave a Reply

Your email address will not be published. Required fields are marked *