繁体   English   中英

从PHP数组转换为C#字典

[英]Converting from PHP array to C# dictionary

我是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");

“最简单”的方法是Dictionary<Object, Object> 由于PHP对数据类型如此松散,因此Object会为您提供更大的灵活性。 然后将.NET 的值是必要的。 就像是:

/* $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添加$results两个相等的值(foo-> Foo,bar-> Bar,baz-> Baz),然后将它们序列化为JSON并得到相同的结果:

[{ “头”:{ “标题”: “测试”, “NUM”:5, “限制”:5}, “数据”:[{ “主”: “主”, “副”: “二次”, “image”:“test.png”,“onclick”:“alert('您点击了主要');”},{“primary”:“Primary”,“secondary”:“Secondary”,“image”: “test.png”,“onclick”:“alert('你点击了主要');”},{“primary”:“Primary”,“secondary”:“Secondary”,“image”:“test.png “,”onclick“:”alert('你点击了主要');“}]}]

与你提出的问题略有不同,但我可能做的事情如下:

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

希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM