您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。