C#数组转Json的一个小问题,希望路过的大佬帮忙看一看

m0_48036920 m0_48036920 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
源代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.Script.Serialization;

namespace ConsoleApp3
{
public class Rootobject
{
public Response response { get; set; }
}
public class Response
{
public string resultCode { get; set; }
public string resultMsg { get; set; }
public string success { get; set; }
public Result result { get; set; }
}
public class Result
{
public string sid { get; set; }
public string valid { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Rootobject> rootobjects = new List<Rootobject>()
{
new Rootobject{ }
};
List<Response> responses = new List<Response>()
{
new Response
{
resultCode = "OK",
resultMsg = "Operation is done successfuly",
success = "true",
}
};
List<Result> results = new List<Result>()
{
new Result
{
sid = "kkdddd",
valid = "true/false"
}
};
string ListStr = JsonHelper.json(rootobjects);
Console.WriteLine(ListStr);

string ListStr1 = JsonHelper.json(responses);
Console.WriteLine(ListStr1);

string ListStr2 = JsonHelper.json(results);
Console.WriteLine(ListStr2);
}
public class JsonHelper
{
/// <summary>
/// json反序列化
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static object jsonDes<T>(string input)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<T>(input);
}
public static string json(object obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj);
}
}
}
}


现在的输出结果是:
[{"response":null}]
[{"resultCode":"OK","resultMsg":"Operation is done successfuly","success":"true","result":null}]
[{"sid":"kkdddd","valid":"true/false"}]

想让它的输出结果是:
{"response":{"resultCode":"OK","resultMsg":"Operation is done successfuly","success":"true","result":{"sid":"kkdddd","valid":"true/false"}}}

就是要有嵌套,希望有大佬指点,是缺少了哪一个步骤,感谢告诉
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
m0_48036920
C# 111,128 成员 642,536 内容