从PHP数组到C#字典的转换
我是C#编程的初学者。请帮助我用PHP将此代码示例重新编写到C#:从PHP数组到C#字典的转换,c#,php,arrays,json,dictionary,C#,Php,Arrays,Json,Dictionary,我是C#编程的初学者。请帮助我用PHP将此代码示例重新编写到C#: 我曾尝试过这样做,但没有成功 Dictionary<string, string> final = new Dictionary<string, string>(); stringArray.Add("header", "data"); Dictionary final=new Dictionary(); 添加(“标题”、“数据”); 最“简单”的方法是使用字典。由于PHP对数据类型的处理非常松散,
我曾尝试过这样做,但没有成功
Dictionary<string, string> final = new Dictionary<string, string>();
stringArray.Add("header", "data");
Dictionary final=new Dictionary();
添加(“标题”、“数据”);
最“简单”的方法是使用字典。由于PHP对数据类型的处理非常松散,因此对象
将为您提供更大的灵活性。然后.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);
class ReturnArg
{
public Dictionary<Object, Object> header = new Dictionary<Object, Object>();
public Dictionary<Object, Object> data = new Dictionary<Object, Object>();
}
测试和工作:
- 您的PHP版本:
- C版本(上图)
我将$results
/results
作为相等的值(foo->foo,bar->bar,baz->baz)添加到两者,然后将两者序列化为JSON并生成相同的结果:
[{“header”:{“title”:“Test”,“num”:5,“limit”:5},“data”:[{“primary”:“primary”,“secondary”:“secondary”,“image”:“secondary”,“image”:“alert”(“您单击了primary”);“},{“primary”:“primary”,“secondary”:“secondary”,“secondary”,“image”:“alert”(“您单击了primary”);“},{“primary”:“secondary”,“secondary”:“secondary”,“image”:“Test.png”,“onclick”:“警报('您单击了主按钮”);“}]}]
与您的要求略有不同,但我可能会这样做:
/* $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);
class ReturnArg
{
public Dictionary<Object, Object> header = new Dictionary<Object, Object>();
public Dictionary<Object, Object> data = new Dictionary<Object, Object>();
}
也可能:
var r = new
{
header = new { title = "Test", num = 5, limit = 5 },
data = new { primary = "Primary", secondary = "Secondary" }
};
希望这能有所帮助。我想我需要的信息比“没有成功”多一点。发生了什么事?对我来说,这两行C#编译正确;我最初唯一缺少的是“使用System.Collections.Generic”“在文件的顶部。实际上,再看一眼,你的C#映射和PHP映射看起来并不相似,但这就是我的答案……我使用的是.NET2.0框架,因此我稍微编辑了一下语法,然后就可以了。谢谢你的帮助:)@Neru:很高兴听到这个消息。并且,为了将来的参考,您可能希望在您的问题上添加.net-2.0
标记,因为这里的大多数问题至少涉及3.5(我在4/4.5中看到更多)
var r = new
{
header = new { title = "Test", num = 5, limit = 5 },
data = new { primary = "Primary", secondary = "Secondary" }
};