5

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");
1
  • I think I need a bit more info than "have had no success". What happened? For me, those two lines of C# compile correctly; the only thing I was initially missing was a "using System.Collections.Generic" at the top of the file. Actually, on second look your C# map and PHP map don't appear similar, but that'll be for my Answer... Commented May 30, 2013 at 13:16

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm using .net 2.0 framework therefore I edit the syntax little bit then it works fine. Thanks for your help :)
@Neru: Glad to hear it. And, for future reference, may want to add the .net-2.0 tag on your questions since most questions on here involve a minimum of 3.5 (and I see a lot more with 4/4.5)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.