Qig*_*ong 1 c# asp.net-mvc json
我想用C#语言生成这样的json字符串
{
"error": "0",
"message": "messages",
"data": {
"version": "sring",
"1": [
{
"keyword": "",
"title": ""
},
{
"keyword": "",
"title": ""
}
],
"2": [
...
],
"3": [
...
]
}
}
Run Code Online (Sandbox Code Playgroud)
这里有一个问题,"1":[{},{}],如何生成这部分?我顺便使用asp.net mvc项目,我想将这个json字符串返回给客户端Web浏览器.
可以使用Dictionary<string, object>数组作为值简单地生成此响应.
public class KeywordTitle
{
public string keyword { get; set; }
public string title { get; set; }
}
public class Response
{
public string error { get; set; }
public string message { get; set; }
public Dictionary<string, object> data { get; set; }
}
var dictionary = new Dictionary<string, object> {
{"version", "sring"}
};
dictionary.Add("1", new []
{
new KeywordTitle { keyword = "", title = "" },
new KeywordTitle { keyword = "", title = "" },
new KeywordTitle { keyword = "", title = "" }
});
JsonConvert.SerializeObject(new Response
{
error = "0",
message = "messages",
data = dictionary
});
Run Code Online (Sandbox Code Playgroud)
它产生:
{
"error" : "0",
"message" : "messages",
"data" : {
"version" : "sring",
"1" : [{
"keyword" : "",
"title" : ""
}, {
"keyword" : "",
"title" : ""
}, {
"keyword" : "",
"title" : ""
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
如果它是您的API,则最好提取version以使所有对象data具有相同类型和类型的键int.
小智 5
从中获取Json.NETNuGet.然后,在您的MVC模型中,在data annotationArray属性上使用它
[JsonProperty(PropertyName="1")]
public string[] YourProperty { get; set }
Run Code Online (Sandbox Code Playgroud)
将PropertyName数据序列化时,将使用该值JSON.
| 归档时间: |
|
| 查看次数: |
3285 次 |
| 最近记录: |