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

m0_48036920 2020-07-02 02:40:36
源代码如下:
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"}}}

就是要有嵌套,希望有大佬指点,是缺少了哪一个步骤,感谢告诉
...全文
2113 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
desperaso 2020-07-02
  • 打赏
  • 举报
回复
引用 2 楼 m0_48036920 的回复:
这个是复制过后出来的结果:
[{"response":{"result":null,"resultCode":"OK","resultMsg":"Operation is done successfuly","success":"true"},"result":{"sid":"kkdddd","valid":"true\/false"}}]
貌似下面这两项有点不正确:
多了第一行第二个单词那里"result":null,"这一项;
最后两个单词那里"true\/false"这里多了个\。


这样定义class看不大明白,
那个"true\/false"没啥意义啊,反序列化对象没影响啊
/// json字符串转成对象
public static T JsonToObject<T>(string json) where T : class
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
{
T result = formatter.ReadObject(stream) as T;
return result;
}
}
m0_48036920 2020-07-02
  • 打赏
  • 举报
回复
已经解决,感谢大佬的提示,就不用大佬辛苦了。
m0_48036920 2020-07-02
  • 打赏
  • 举报
回复
这个是复制过后出来的结果: [{"response":{"result":null,"resultCode":"OK","resultMsg":"Operation is done successfuly","success":"true"},"result":{"sid":"kkdddd","valid":"true\/false"}}] 貌似下面这两项有点不正确: 多了第一行第二个单词那里"result":null,"这一项; 最后两个单词那里"true\/false"这里多了个\。 大佬如果不忙的情况下麻烦您帮忙看一下。 纯小白,问题较蠢,还请见谅。
desperaso 2020-07-02
  • 打赏
  • 举报
回复

。。。。。。。
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
。。。。。。。。。。。


。。。。。。。。。
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; }
}

[DataContract]
public class Temp
{
[DataMember]
public Response response;
[DataMember]
public Result result;
}
public static List<Temp> temps = new List<Temp>();

。。。。。。。。。。。。
。。。。。。。。。。。。。

private void button1_Click(object sender, EventArgs e)
{
Response responses = new Response()
{
resultCode = "OK",
resultMsg = "Operation is done successfuly",
success = "true",
};

Result results = new Result()
{
sid = "kkdddd",
valid = "true/false"
};


Temp temp = new Temp()
{
response = responses,
result = results
};
temps.Add(temp);

string ListStr = ObjectToJson(temps);
MessageBox.Show(ListStr);
}

public static void ObjectToJson<T>(T t, string path) where T : class
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate))
{
formatter.WriteObject(stream, t);
}
}

public static string ObjectToJson<T>(T t) where T : class
{
DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(T));
using (MemoryStream stream = new MemoryStream())
{
formatter.WriteObject(stream, t);
string result = System.Text.Encoding.UTF8.GetString(stream.ToArray());
return result;
}
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧