The easiest way is Dictionary<Object, Object> . Because PHP is so free of data types, Object will give you more flexibility. Then .NET introduced the value field as needed. Sort of:
/* $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 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 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 that this is by far not the most optimized one, but makes it closer to what you are working with.
From there, you can use the library (e.g. Newtsonsoft Json ) and serialize the information.
JsonConvert.SerializeObject(final);
Tested and working:
I added $results / results as equal values (foo-> Foo, bar-> Bar, baz-> Baz), and then serialized in both JSON and the result:
[{"heading": {"name": "Test", "number": 5, "limit": 5}, "data": [{"primary": "Primary", "secondary": "Secondary", "image": "test.png", "onclick": "alert (" You clicked on Primary ");" }, {"primary": "Primary", "secondary": "Secondary", image ":" test.png "," onclick ":" alert ('You clicked on Primary'); "}, {" primary ":" Primary "," secondary ":" Secondary "," image ":" test.png "," onclick ":" alert ('You clicked on Primary'); "}]}}