您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在C#中拼接Json串,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
string uid = "22"; var abcObject = new { AccessKey = 11, CustomerNo = uid, mc = "33", qd = "44", mr = "55", insertDate = DateTime.Now }; string serJson = JsonConvert.SerializeObject(abcObject);
StringBuilder str = new StringBuilder(); str.Append("{"); str.Append("AccessKey:\"" + 11 + "\","); str.Append("mc:\"" + 22 + "\","); str.Append("qd:\"" + 33 + "\""); str.Append("}"); string serJson = str.ToString();
string json = "{\"speed\":" + speed + "," + "\"direction\":" + direction + "}"; TODO:输出 { "speed": 591, "direction": 0 }
"{\"Bool_Type\":\"Bool\",\"Int_Type\":6666666,\"Float_Type\": 66.99,\"String_Type\":\"这是String类型\",\"Vector2_Type\":{\"x\":666.0,\"y\":666.0},\"Vector3_Type\":{\"x\":666.0,\"y\":666.0,\"z\":666.0}}";
string mc = "22"; string id = "11"; string serJson = string.Format("[{{ AccessKey:\"{0}\",mc:\"{1}\"}},{{ AccessKey:\"{2}\",mc:\"{3}\"}}]", id, mc, "33", "44");
首先下载Newtonsoft.Json,增加引用using Newtonsoft.Json.Linq;
把jobject的内容提取出来,
//Jobject的内容格式如下: { "code": 200, "msg": "SUCCESS", "data": { "id": "12345678", "name": "张三", "sex": "男", "result": { "access_token": "49d58eacd7811e463429a1ae10b42173", "user_info": [{ "school": "社会大学", "major": "软件开发", "education": "本科", "score": 97 }, { "school": "湖南大学", "major": "软件工程", "education": "研究生", "score": 100 }] } } }
可放到json官网在线JSON校验格式化工具里解析。
代码如下:
1,新建类:
public class UserInfo { public string id { get; set; } public string name { get; set; } public string sex { get; set; } public string access_token { get; set; } public string school { get; set; } public string major { get; set; } public string education { get; set; } public string score { get; set; } }
2,获取值:
JObject result = new JObject();//假设result为数据结构 UserInfo userinfo = new UserInfo(); userinfo.id = result["data"].Value<string>("id");//id userinfo.name = result["data"].Value<string>("name"); //name userinfo.sex = result["data"].Value<string>("sex"); //sex userinfo.access_token= result["data"]["result"]["access_token"].ToString();//access_token JArray res = result["data"]["result"].Value<JArray>("user_info"); JObject obj = JObject.Parse(res[0].ToString());//只获取数据结构中第一个userinfo里的数据信息 userinfo.school = obj.Value<string>("school"); //schoool userinfo.major = obj.Value<string>("major");//major userinfo.education = obj.Value<string>("education");//education userinfo.score= obj.Value<string>("score");//score
关于怎么在C#中拼接Json串就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。