c#序列化json文件为字符串-更改json对象内容

c#序列化json文件为字符串-更改json对象内容

 public static dynamic Serializ(string send)
        {
            try
            {
                string path = Application.StartupPath + $@"\{send}";
                var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var streamReader = new StreamReader(file);
                string jsonStr = streamReader.ReadToEnd();
                dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
                string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
                return output;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "/r/n" + e.StackTrace);
                return null;
            }
        }
   public static dynamic Changes(string send,string where_name,string Num)
        {
            try
            {
                string path = Application.StartupPath + $@"\{send}"; //JSON路径
                var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var streamReader = new StreamReader(file);
                string jsonStr = streamReader.ReadToEnd();
                dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
                jsonObj["destinations"][0]["locationName"] = $"{where_name}";
                jsonObj["wrappingSequence"] = Num; 
                string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
                return output;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "/r/n" + e.StackTrace);
                return null;
            }
        }

 

posted @ 2021-11-19 17:06  txwtech  阅读(271)  评论(0编辑  收藏  举报