dongzhizhai4070 2013-05-30 13:10 采纳率: 0%
浏览 85
已采纳

从PHP数组转换为C#字典

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");
  • 写回答

2条回答 默认 最新

  • dte49889 2013-05-30 13:18
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度