2013-05-30 85 views
4

我是C#編程中的初學者。請幫我用PHP重寫這段代碼示例到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"); 
+0

我想我需要一些比「沒有成功」更多的信息。發生了什麼?對我而言,這兩行C#編譯正確;我最初唯一缺少的是在文件頂部使用「System.Collections.Generic」。其實,第二次看你的C#地圖和PHP地圖不會出現類似的情況,但這將是我的答案... – Katana314

回答

4

「最簡單」的方法是Dictionary<Object, Object>。由於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和resul t中相同:

[{「header」:{「title」:「Test」,「num」:5,「limit」:5},「data」:[{「primary」:「主要「,」次要「:」次要「,」圖像「:」test.png「,」onclick「:」警報('您點擊主要');「},{」primary「:」Primary「輔助「:」次要「,」圖像「:」test.png「,」onclick「:」警報('您點擊主要');「},{」primary「:」Primary「,」secondary「中級「,」圖像「:」test.png「,」onclick「:」alert('您點擊了主');「}]}]

+0

+1 - 謝謝,我會試試:) –

+0

必須爲你工作。 ;-) –

+1

我使用.net 2.0框架,因此我編輯語法有點,然後它工作正常。感謝您的幫助:) –

0

與你所問的略有不同, ð可能做這樣的事情:

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

希望有所幫助。