从PHP数组转换为C#字典 [英] Converting from PHP array to C# dictionary
问题描述
我在C#编程初学者。请帮我在PHP这个code样品重新写C#:
< PHP
$最后=阵列('标题'=>数组(),'数据'=>阵());
$最后的['标题'] =阵列('标题'=>'测试','民'=> 5,限价=大于5); 的foreach($结果$名=> $数据)
{
$最后的['数据'] [] =阵列('初级'=>'主','二级'=>'二次','形象'=>'test.png','的onclick => 警报('你点击主');');
} 标题(内容类型:应用程序/ JSON');
回声json_en code(阵列($最终));
?>
我试图做这样的事情,但都没有成功。
词典<字符串,字符串>最后=新词典<字符串,字符串>();
stringArray.Add(头,数据);
在最简单的方法将是词典<对象,对象>
。由于PHP是数据类型那么松,一个对象
会给你更多的灵活性。然后将.NET值作为必要 框。是这样的:
/ * $决赛* /
IDictionary的<对象,对象>最后=新词典<对象,对象>();/ * $决赛[标题] * /
//通过保持这种分离然后将其加入到最后的,您就不必
//每一个需要引用它时施放它,因为它被存储
//为Object
IDictionary的<对象,对象>标题=新词典<对象,对象> {
{称号,测试},
{NUM,5},
{限制,5}
};
//上述短手声明是一样的操作的方式:
// header.Add(称号,测试);
// header.Add(民,5);
// header.Add(限制,5);
final.Add(头,头);/ * $决赛[数据] * /
IList的<对象>数据=新的List<对象>();
//不知道在哪里`results`从何而来,但我会假设它是另一种类型的
// IDictionary的&所述的T1,T2和GT;
的foreach(KeyValuePair<对象,对象> KVP的结果)
{
data.Add(新字典<对象,对象> {
{主,主},
{次要,次要},
{图像,test.png},
{的onclick,警报('你点击主'); }
});
}
final.Add(数据,数据);
只要记住,这是肯定的不可以最优化的,但确实使最接近你正在使用的东西。
从那里,你可以使用库(如 Newtsonsoft的Json )和序列化的信息。
JsonConvert.SerializeObject(最后一次);
测试和工程:
- 您的PHP版本: http://ideone.com/NzfIj4
- C#版本(上图)
我添加 $结果
/ 结果
来既作为相等值(foo->富,酒吧,>酒吧,baz->巴兹),那么序列既JSON和导致相同
[{\"header\":{\"title\":\"Test\",\"num\":5,\"limit\":5},\"data\":[{\"primary\":\"Primary\",\"secondary\":\"Secondary\",\"image\":\"test.png\",\"onclick\":\"alert('You点击主');},{主:主,次要:中学,形象:test.png,onclick事件:警报('你点击主');},{主:主,次要:中学,形象:test.png,onclick事件:警报('你点击主'); }]}]
块引用>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");
解决方案The "easiest" method would be a
Dictionary<Object, Object>
. Since PHP is so loose with data types, anObject
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:
- Your PHP Version: http://ideone.com/NzfIj4
- The C# Version (above)
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');"}]}]
这篇关于从PHP数组转换为C#字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!