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
str = "[{type:'salad', qt:0, price:0.25},{type:'cheese', qt:0, price:0.45},{type:'bacon', qt:0, price:0.75},{type:'meat', qt:0, price:4.00},]"
str = str.replace(/'/g, '"');
str = str.replace(/type/g, '"type"');
str = str.replace(/qt/g, '"qt"');
str = str.replace(/price/g, '"price"');
str = str.replace(",]","]")
arry = JSON.parse(str)
console.log(arry)
Related Topics
Pdoexception "Could Not Find Driver"
Convert a PHP Object to an Associative Array
How to Convert an Image to Base64 Encoding
How to Solve PHP Error 'Notice: Array to String Conversion In...'
Get the First Element of an Array
Calling Closure Assigned to Object Property Directly
Delimiter Must Not Be Alphanumeric or Backslash and Preg_Match
500 Internal Server Error For PHP File Not For Html
How to Secure Database Passwords in PHP
How to Extract Img Src, Title and Alt from HTML Using PHP
Simplest Way to Detect a Mobile Device in PHP
Preferred Method to Store PHP Arrays (Json_Encode VS Serialize)
How to Get a Hex Dump of a String in PHP