C#数组转Json的一个小问题,希望路过的大佬帮忙看一看
源代码如下:
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"}}}
就是要有嵌套,希望有大佬指点,是缺少了哪一个步骤,感谢告诉
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"}}}
就是要有嵌套,希望有大佬指点,是缺少了哪一个步骤,感谢告诉
...全文
相关推荐
C#构造Json数组
希望本文能帮助你更好地理解和掌握C#中处理JSON数据的方法。
C# WinForm一维数组转二维数组及保存实践指南
序列化是一种将数据结构或对象状态转换为可存储格式的方法,如XML、JSON或二进制格式,以便可以在需要时重构该数据结构。对于二维数组来说,序列化尤为重要,因为它允许数组跨越不同的系统边界进行数据交换。序列化后的数组可以轻易地通过网络传输到远程服务器或存档到磁盘,实现数据的持久化。
json转数组然后遍历
#### 二、JSON转数组在本例中,我们需要将一个包含设备坐标信息的JSON对象转换为数组。
C#通过post提交json字符串
其中,JSON(JavaScript Object Notation)作为轻量级的数据交换格式被广泛使用。本文将详细介绍如何使用C#语言通过POST方式提交JSON字符串,并提供一个完整的示例代码。
C#实现Json转Unicode的方法
以下是一种C#实现Json转Unicode的方法,该方法可以将带有JSON格式的文本转换为正常文本。


