一段处理json的C#代码

时间:2023-03-10 05:16:47
一段处理json的C#代码

服务器端:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public ActionResult GetGatherData()
{
    IList<M_Gather> list = gatherpolygonService.GetAll();

    JArray jarr = new JArray();
    foreach (var g in list)
    {
        jarr.Add(new JObject()
        {
            new JProperty("ID",g.GATHERID),
            new JProperty("POINTNAME",g.POINTNAME),
            new JProperty("DEVICEID",g.DEVICEID),
        });
    }
    JObject jobj = new JObject()
    {
        new JProperty("totalCount",getall.Count()),
        new JProperty("topics",jarr)
    };

    return Content(JsonConvert.SerializeObject(jobj), "application/json");
}

得到的json如下:

{
totalCount:10,
topics:[
{ID:"","POINTNAME":"","DEVICEID":""},...
]
}

相关文章:

http://blog.****.net/leftfist/article/details/43155295