Author Posts

October 4, 2016 at 5:39 am

I am creating a class to build a Json object I would like to call another class as an array how can I do that

Class A
{
[string] $aObject
[B] $aObject // this didn't worked for me
}

Class B
{
[string] $bObject
}

Class c
{
[string] $cObject
}

I need class B and C as arrays in class A

I am trying to loop this recursively either by Json or by creating an object

{
"cluster": [
{
"id": "cluster1.1",
"color": "blue",
"segment": [
{
"id": "segment1.1",
"color": "green"
}
]
},
{
"id": "cluster1.2",
"color": [
"blue",
"red"
],
"segment": [
{
"id": "segment1.2",
"color": "Yellow"
}
]
},
{
"id": "cluster1.3",
"color": "Orange",
"segment": [
{
"id": "cluster1.3",
"color": "black"
},
{
"id": "cluster1.4",
"color": "Green"
},
{
"id": "cluster1.5",
"color": "red"
}
]
},
{
"id": "cluster1.4",
"color": [
"blue",
"red"
],
"segment": [
{
"id": "cluster1.4",
"color": "red"
},
{
"id": "cluster1.5",
"color": "blue"
},
{
"id": "cluster1.6",
"color": "Yellow"
}
]
}
]
}

October 4, 2016 at 2:39 pm

You don't need to build this. ConvertFrom-Json already exists.

Example:

$data = @'
{
"cluster": [
{
"id": "cluster1.1",
"color": "blue",
"segment": [
{
"id": "segment1.1",
"color": "green"
}
]
},
{
"id": "cluster1.2",
"color": [
"blue",
"red"
],
"segment": [
{
"id": "segment1.2",
"color": "Yellow"
}
]
},
{
"id": "cluster1.3",
"color": "Orange",
"segment": [
{
"id": "cluster1.3",
"color": "black"
},
{
"id": "cluster1.4",
"color": "Green"
},
{
"id": "cluster1.5",
"color": "red"
}
]
},
{
"id": "cluster1.4",
"color": [
"blue",
"red"
],
"segment": [
{
"id": "cluster1.4",
"color": "red"
},
{
"id": "cluster1.5",
"color": "blue"
},
{
"id": "cluster1.6",
"color": "Yellow"
}
]
}
]
}
'@

$json = ConvertFrom-Json $data
$json.cluster

Results:

id         color       segment                                                                                    
--         -----       -------                                                                                    
cluster1.1 blue        {@{id=segment1.1; color=green}}                                                            
cluster1.2 {blue, red} {@{id=segment1.2; color=Yellow}}                                                           
cluster1.3 Orange      {@{id=cluster1.3; color=black}, @{id=cluster1.4; color=Green}, @{id=cluster1.5; color=red}}
cluster1.4 {blue, red} {@{id=cluster1.4; color=red}, @{id=cluster1.5; color=blue}, @{id=cluster1.6; color=Yellow}}