这就是我希望我的对象看起来像:
{
"cluster": "testcluster",
"instance": "i-03f8f8c9e7febab21",
"instype": "r4.8xlarge",
"AZ": "us-east-1e",
"env": "test",
"cpus": "32",
"memory": "256G",
"kernel": "4.4.0-96-generic",
"info": "cpu usage stats",
"unit": "percent",
"cpustats": [
{"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"sys", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"idle", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"softirq", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"intr", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"steal", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]},
{"metric":"user", "values":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]}
]
}
如何在php中构建?
答案 0 :(得分:0)
PHP具有管理JSON对象的特定功能:json_encode和json_decode
用于将Object转换为JSON字符串
private void button1_Click_2(object sender, EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
// Read the files
foreach (String file in openFileDialog1.FileNames)
{
// Create a PictureBox.
try
{
Panel panel = new Panel();
PictureBox pb = new PictureBox();
TextBox tb = new TextBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 200;
pb.Width = 200;
pb.Image = loadedImage;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
tb.Text = openFileDialog1.FileName;
//panel.Controls.Add(pb);
panel.Controls.Add(tb);
panel.Height = 200;
panel.Width = 200;
flowLayoutPanel1.Controls.Add(panel);
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +
"Details (send to Support):\n\n" + ex.StackTrace
);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
}
}
它用于从JSON字符串中检索原始对象
private void button2_Click(object sender, EventArgs e)
{
foreach(TextBox tb in TextBoxes)
{
File.Copy(tb.Text, dest);
}
}
我改变了JSON的样子:
<?php
/** You can also use Arrays (Ex. $myArray = array(...)) */
$myObject = new StdClass();
$myObject->newKey = "newValue";
$myObject->otherKey = "otherValue";
$myObject->nestedArrays = array(
'array1' => array(
'key1' => 'value1',
'key2' => 'value2',
/** ... OTHER KEY-VALUE ... */
'keyN' => 'vakyeN'
),
'array2' => array(
'key1' => 'value1',
'key2' => 'value2',
/** ... OTHER KEY-VALUE ... */
'keyN' => 'vakyeN'
)
);
/** ... OTHER CODE ... */
$myJSONString = json_encode($myObject);
print_r($myJSONString);
/** OUTPUT
* {
* "newKey":"newValue",
* "otherKey":"otherValue",
* "nestedArrays":{
* "array1":{
* "key1":"value1",
* "key2":"value2",
* "keyN":"vakyeN"
* },
* "array2":{
* "key1":"value1",
* "key2":"value2",
* "keyN":"vakyeN"
* }
* }
* }
*/
?>
<?php
$myObject = json_decode($myJSONString);
?>
{
"cluster": "testcluster",
"instance": "i-03f8f8c9e7febab21",
"instype": "r4.8xlarge",
"AZ": "us-east-1e",
"env": "test",
"cpus": "32",
"memory": "256G",
"kernel": "4.4.0-96-generic",
"info": "cpu usage stats",
"unit": "percent",
"cpustats": {
"user":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
"sys":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
"idle":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
"softirq":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
"intr":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891],
"steal":[1874,1857,1884,1869,1909,1912,1901,1880,1883,1889,1891]
}
}