Neru-J
Neru-J

Reputation: 1623

Converting from PHP array to C# dictionary

I am a beginner in C# programing. Please help me re-write this code sample in PHP to C#:

<?php
  $final = array('header' => array(), 'data' => array());
  $final['header'] = array('title' => 'Test', 'num' => 5, 'limit' => 5);

  foreach ($results as $name => $data)
  {
    $final['data'][] = array('primary' =>'Primary','secondary' => 'Secondary','image' => 'test.png','onclick' => 'alert('You clicked on the Primary');');
  }

  header('Content-type: application/json');
  echo json_encode(array($final));
?>

I have tried to do something like this, but have had no success.

Dictionary<string, string> final = new Dictionary<string, string>();
stringArray.Add("header", "data");

Upvotes: 5

Views: 7308

Answers (2)

Brad Christie
Brad Christie

Reputation: 101614

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');"}]}]

Upvotes: 5

anthr
anthr

Reputation: 719

Slightly different to what you were asking but i'd probably do something like:

class ReturnArg
{
    public Dictionary<Object, Object> header = new Dictionary<Object, Object>();
    public Dictionary<Object, Object> data = new Dictionary<Object, Object>();
}

Then (in MVC controller?):

public JsonResult GetRevisions(int id)
{
    ReturnArg r = new ReturnArg();

    r.header.Add("title", "Test");
    r.header.Add("num", 5);
    r.header.Add("limit", 5);

    r.data.Add("primary", "Primary");
    ...

    return Json(r);
}

Also possible:

var r = new
{
    header = new { title = "Test", num = 5, limit = 5 },
    data = new { primary = "Primary", secondary = "Secondary" }
};

Hope that helps.

Upvotes: 0

Related Questions