在C#中拼接字符串有几种方法

1. 利用  JsonConvert.SerializeObject方法 (Nuget获取Newtonsoft.Json Package)

            string uid = "22";

            var abcObject = new

            {

                AccessKey = 11,

                CustomerNo = uid,

                mc = "33",

                qd = "44",

                mr = "55",

                insertDate = DateTime.Now

            };

            string serJson = JsonConvert.SerializeObject(abcObject);

2. 利用StringBuilder

           StringBuilder str = new StringBuilder();

            str.Append("{");

            str.Append("AccessKey:\"" + 11 + "\",");

            str.Append("mc:\"" + 22 + "\",");

            str.Append("qd:\"" + 33 + "\"");

            str.Append("}");

            string serJson = str.ToString();

3. 直接拼接字符串

string serJon = "{AccessKey:\"" + 11 + "\",mc:\"" + 22 + "\"}";

4. 利用StringFormat

  string mc = "22";
 string id = "11";
 string serJson = string.Format("[{{ AccessKey:\"{0}\",mc:\"{1}\"}},{{ AccessKey:\"{2}\",mc:\"{3}\"}}]", id, mc, "33", "44");

其他常用方式:

1.运用StringBuilder拼接
StringBuilder json = new StringBuilder();
json.append("{");
json.append(""uuid":" + """ + uuid + "",");
json.append(""phone":" + """ + list.get(i).get("DEST_NUMBER") + "",");
json.append(""tempTypeId":"6",");
json.append(""tempStatus":"0",");
json.append(""account":" + """ + list.get(i).get("DEST_NUMBER") + "",");
json.append(""amount":" + """ + Integer.parseInt(list.get(i).get("AMOUNT") + "") / 100 + "",");
json.append(""result":"0",");
json.append(""source":"JF",");
json.append(""first":"",");
json.append(""url":"",");
json.append(""remark":""");
json.append("}");
String jsonstr = json.toString();

2.运用JSONArray(JsonObject)
JSONObject jo = new JSONObject(); // 对象{}
JSONArray json = new JSONArray();// 数组[]
jo.put("id", Convert.convertNull(hm.get("cost_center_id")));
jo.put("Pid", Convert.convertNull(hm.get("parent_center_id")));
jo.put("name", Convert.convertNull(hm.get("cost_center_name")));
jo.put("isParent", Long.parseLong(Convert.convertNull(hm.get("count"))) > 0 ? true : false);
jo.put("levelId",Convert.convertNull(hm.get("level")));
String jsonObj = jo.toString();
json.put(jo);
String jsonArr = json.toString();

json解析:
1.List idList = (List) JSONUtil.deserialize(agreementIds);

2.运用JSONObject

注:
json.append("msg", "a1")
json.append("msg", "a2")
{"msg":["a1", "a2"]}
---数组追加

json.put("msg", "a1")
json.put("msg", "a2")
{"msg":"a2"}
---ojbect替换