【问题标题】:Converting from PHP array to C# dictionary从 PHP 数组转换为 C# 字典
【发布时间】:2013-05-26 02:02:36
【问题描述】:

我是 C# 编程的初学者。请帮我将 PHP 中的代码示例重写为 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));
?>

我尝试过这样做,但没有成功。

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

【问题讨论】:

  • 我认为我需要比“没有成功”更多的信息。发生了什么?对我来说,这两行 C# 编译正确;我最初唯一缺少的是文件顶部的“使用 System.Collections.Generic”。实际上,第二次看你的 C# 映射和 PHP 映射看起来并不相似,但这将是我的答案......

标签: c# php arrays json dictionary


【解决方案1】:

“最简单”的方法是Dictionary&lt;Object, Object&gt;。由于 PHP 的数据类型非常松散,Object 会给你更多的灵活性。然后 .NET 将根据需要 box 该值。比如:

/* $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);

请记住,这肯定不是最优化的,但确实使它最接近您正在使用的内容。

从那里,您可以使用库(如Newtsonsoft Json)并序列化信息。

JsonConvert.SerializeObject(final);

经过测试和工作:

我将 $results/results 添加为相等的值 (foo->Foo,bar->Bar,baz->Baz) 然后将两者序列化为 JSON 并产生相同的结果:

[{"header":{"title":"Test","num":5,"limit":5},"data":[{"primary":"Primary","secondary":" Secondary","image":"test.png","onclick":"alert('你点击了 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 the Primary');"}]}]

【讨论】:

  • 我正在使用 .net 2.0 框架,因此我稍微编辑了语法,然后它就可以正常工作了。感谢您的帮助:)
  • @Neru:很高兴听到这个消息。而且,为了将来参考,可能需要在您的问题上添加.net-2.0 标签,因为这里的大多数问题都涉及至少 3.5(我看到更多 4/4.5)
【解决方案2】:

与您的要求略有不同,但我可能会这样做:

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

然后(在 MVC 控制器中?):

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);
}

也可以:

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2014-02-19
    • 2015-05-05
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2017-07-21
    • 2023-03-15
    • 2011-09-28
    • 2021-08-10
    相关资源
    最近更新 更多