How to Convert Json String to Array

Convert JSON string to array of JSON objects in Javascript

Using jQuery:

var str = '{"id":1,"name":"Test1"},{"id":2,"name":"Test2"}';
var jsonObj = $.parseJSON('[' + str + ']');

jsonObj is your JSON object.

How to convert JSON string to arrays

This is what you need, json_decode($json,true);

<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = json_decode($json,1);
print_r($array[0]);
?>

DEMO: https://3v4l.org/JZQCn

OR use it as a parsable string representation of a variable with var_export()

<?php
$json = '[{"name":"Menu","sub":[{"name":"Menu 2","url":"menu-2.php"}]}]';
$array = var_export(json_decode($json,1)[0]);
print($array);
?>

DEMO: https://3v4l.org/rLA9R

What is best way to convert json string into array of objects?

I recommend to install Newtonsoft.Json via the NuGet package manager. Then decorate the members of the POCO class with the JsonProperty attribute to tell the serializer which property matches to a specific JSON key:

public class MyJsonObject
{
[JsonProperty("Value")]
string Value { get; set; }

[JsonProperty("Name")]
string Name { get; set; }
}

You can the deserialize the JSON to a corresponding object instance:

var jsonString = "[{\"Value\": \"1\", \"Name\": \"One\"}, {\"Value\": \"2\", \"Name\": \"Two\"}]";
List<MyJsonObject> myJsonObjects = JsonConvert.DeserializeObject<List<MyJsonObject>>(jsonString);

Convert array of JSON String to array of object in Python

Now solution looks wired but this works and will be useful for you in optimization. Convert the complete list into str then remove all ' single commas with str function and the apply json loads, hurray this has got worked for me.

data = ['[1,2,3]', '[4,5,6]', '[7,8,9]']
r = str(data).replace("'",'')

import json
data = json.loads(r)

now your data will be of list of list without looping. You can achieve this.

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

PHP How to convert array json string to array?

A nester foreach can solve this for you

<?php

$data = [
[ 143=>166 ],
[ 93=>49 ]
];

$return = [];
foreach ($data as $d)
{
foreach ($d as $k=>$v) $return[$k] = $v;
unset($v);
} unset($d);

var_dump($return); // array(2) { [143]=> int(166) [93]=> int(49) }

convert a JSON to a string array in typescript

If the response is what you included then it's simple:

JSON.parse(data.text()).forEach(item => {
console.log(item.name);
});

As your body is an array of objects of this interface:

interface ObjectInResponseArray {
name: string;
}

How to convert this non-JSON string to array of objects?

Although fixing the saved data is the best bet, this will work but its very "hacky".

Simply replace all of the keys with strings wrapped in double quotes and replace the single quotes with double quotes then use JSON.parse