Generate Json String from Multidimensional Array Data

Generate json string from multidimensional array data

The simplest way would probably be to start with an associative array of the pairs you want:

$data = array("myfirstvalue" => "myfirsttext", "mysecondvalue" => "mysecondtext");

then use a foreach and some string concatenation:

$jsontext = "[";
foreach($data as $key => $value) {
$jsontext .= "{oV: '".addslashes($key)."', oT: '".addslashes($value)."'},";
}
$jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma
$jsontext .= "]";

Or if you have a recent version of PHP, you can use the json encoding functions built in - just be careful what data you pass them to make it match the expected format.

Multidimensional Array to JSON (and back) in Java

You can create a Map of AreaHash objects then convert it to JSON string.

Map<String, AreaHash> data = new HashMap<>();
data.put("1", new AreaHash(...));
data.put("2", new AreaHash(...));
data.put("3", new AreaHash(...));

String jsonString = new Gson().toJson(data);
System.out.println(jsonString);

Output (Pretty printed)

{
"1": {
"Name": "Test",
"Owner": "gold",
"X": "10",
"Y": "10",
"Z": "0"
},
"2": {
"Name": "Test",
"Owner": "silver",
"X": "10",
"Y": "10",
"Z": "-10"
},
"3": {
"Name": "Test",
"Owner": "test",
"X": "10",
"Y": "10",
"Z": "-10"
}
}

Also, take note that your desired output is not valid JSON.

[
"Name": true,
"Owner": "gold",
"X": "10",
"Y": "15",
"Z": "-1000"
] // this will not parse

How to convert PHP Multidimensional Array to Json so it works as Json POST request?

To make json_encode generate an array of json objects, the key bar2 needs to contain an array of arrays, i.e.

$myArray = array(
"bar2" => array(
array(
"hello" => "world"
),
),
);

Updating your original array to the following format will generate a json object with bar2 returning an array of objects.

$myArray = array(
"bar1" => "sample",
"bar2" => array(
array(
"bar1" => "sample",
"bar2" => "sample",
"bar3" => "111111111",
"bar4" => "sample",
"bar5" => [
"bar6" => "varr",
"bar7" => array(
-70.111111111111111,
-33.111111111111111
)
]
)
),
"bar8" => 11111
);

The following array will generate the following json.

{
"bar1": "sample",
"bar2": [
{
"bar1": "sample",
"bar2": "sample",
"bar3": "111111111",
"bar4": "sample",
"bar5": {
"bar6": "varr",
"bar7": [
-70.11111111111111,
-33.111111111111114
]
}
}
],
"bar8": 11111
}

Demo source.

Create multidimensional arrays on json string

This function take the daten['data'] array and create a list entry with 3 possitions array in first position and prodDatum value in second

function createHochOfenChart(daten){
var barValues = daten["data"].map(function (e){
var valus = new Array();
valus.push([e.reSoll, e.reIst, e.reVorrat])
valus.push(e.prodDatum);
return valus
})
return barValues
}

Is what you wanted?

Creating a JSON object from multidimensional array stored as string

It's pretty jerry-rigged, but you could do this:

$string = json_encode( $database_value ); // same as you're using now
$string = str_replace( '"', '', $string ); // get rid of the quotes

// wrap them back around 'coordinates'
$string = str_replace( 'coordinates', '"coordinates"', $string );

How to create a JSON Array from a two-dimensional array in Ruby

Your desired result is still a Ruby hash. To transform it call map on the array and create a hash for each entry:

a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]

a.map{ |e| {address: e[0], value: e[1]} }

This returns the desired result.

If you want to create a JSON string, require json and do the following:

require 'json'

a = [
["01fe237f804a5eff182dcded115c37d3", 0.0140845],
["026e5f1f7f026bf3c763523206aa44bf", 0.03448275],
["04a1c3c79773bd1ecc0372a991adc815", 0.04617604]
]

a.map{|e| {address: e[0], value: e[1]} }.to_json

This will encode the result to the following string:

"[{\"address\":\"01fe237f804a5eff182dcded115c37d3\",\"value\":0.0140845},{\"address\":\"026e5f1f7f026bf3c763523206aa44bf\",\"value\":0.03448275},{\"address\":\"04a1c3c79773bd1ecc0372a991adc815\",\"value\":0.04617604}]"


Related Topics



Leave a reply



Submit