The "easiest" method would be a Dictionary<Object, Object>
. Since PHP is so loose with data types, an Object
would give you more flexibility. Then .NET would box the value as necessary. Something like:
/* $final */
IDictionary<Object, Object> final = new Dictionary<Object, Object>();
/* $final["header"] */
// by keeping this separated then joining it to final, you avoid having
// to cast it every time you need to reference it since it's being stored
// as an Object
IDictionary<Object, Object> header = new Dictionary<Object, Object> {
{ "title", "Test" },
{ "num", 5 },
{ "limit", 5 }
};
// above short-hand declaration is the same as doing:
// header.Add("title", "Test");
// header.Add("num", 5);
// header.Add("limit", 5);
final.Add("header", header);
/* $final["data"] */
IList<Object> data = new List<Object>();
// not sure where `results` comes from, but I'll assume it's another type of
// IDictionary<T1,T2>
foreach (KeyValuePair<Object, Object> kvp in results)
{
data.Add(new Dictionary<Object, Object> {
{ "primary", "Primary" },
{ "secondary", "Secondary" },
{ "image", "test.png" },
{ "onclick", "alert('You clicked on the Primary');" }
});
}
final.Add("data", data);
Just keep in mind, this is certainly not the most optimized, but does make it closest to what you're working with.
From there, you can use a library (like Newtsonsoft Json) and serialize the information.
JsonConvert.SerializeObject(final);
Tested and works:
I added $results
/results
to both as equal values (foo->Foo,bar->Bar,baz->Baz) then serialized both to JSON and result in the same:
[{"header":{"title":"Test","num":5,"limit":5},"data":[{"primary":"Primary","secondary":"Secondary","image":"test.png","onclick":"alert('You clicked on the Primary');"},{"primary":"Primary","secondary":"Secondary","image":"test.png","onclick":"alert('You clicked on the Primary');"},{"primary":"Primary","secondary":"Secondary","image":"test.png","onclick":"alert('You clicked on the Primary');"}]}]