2017-05-10 33 views
-1

我建立在視覺工作室2015年C#WPF應用程序,需要能夠從應用C#我如何使用文本框編輯JSON的具體線路

編輯配置文件在我有一個應用程序帶有一些文本框的TabItem,這些文本框由來自S3 Bucket的配置文件請求填充。

文本框將填入的選擇更改事件的基礎從ComboBox

這裏的問題是能夠編輯特定的路線,每次我試圖我覆蓋了整個配置只新的數據,而不是編輯當前數據。

這是我正在使用的JSON結構。

[ 
    { 
    "ClientId": 23, 
    "EmailVersionId": "u74hdta58jmf", 
    "ClientName": "ClientName", 
    "emailTemplateConfigs": [ 
     { 
     "emailTypeName": "EmailName", 
     "emailTypeId": 15, 
     "subject": "subject", 
     "fromAddress": "from address", 
     "headerFooterReplacements": [ 
      { 
      "placeholder": "##HEADER_PHONENUMBER##", 
      "value": "0000 000 000" 
      }, 
      { 
      "placeholder": "##HTML_TITLE##", 
      "value": "Title" 
      }, 
      { 
      "placeholder": "##EMAIL_HEADING##", 
      "value": "Heading" 
      } 
     ] 
     }, 
     { 
     "emailTypeName": "EmailName", 
     "emailTypeId": 29, 
     "subject": "subject", 
     "fromAddress": "from address", 
     "headerFooterReplacements": [ 
      { 
      "placeholder": "##HEADER_PHONENUMBER##", 
      "value": "0000 000 000" 
      }, 
      { 
      "placeholder": "##HTML_TITLE##", 
      "value": "Title" 
      }, 
      { 
      "placeholder": "##EMAIL_HEADING##", 
      "value": "Heading" 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "ClientId": 33, 
    "EmailVersionId": "jcna53sj231h", 
    "ClientName": "ClientName", 
    "emailTemplateConfigs": [ 
     { 
     "emailTypeName": "EmailName", 
     "emailTypeId": 12, 
     "subject": "subject", 
     "fromAddress": "from address", 
     "headerFooterReplacements": [ 
      { 
      "placeholder": "##HEADER_PHONENUMBER##", 
      "value": "0000 000 000" 
      }, 
      { 
      "placeholder": "##HTML_TITLE##", 
      "value": "Title" 
      }, 
      { 
      "placeholder": "##EMAIL_HEADING##", 
      "value": "Heading" 
      } 
     ] 
     }, 
     { 
     "emailTypeName": "EmailName", 
     "emailTypeId": 49, 
     "subject": "subject", 
     "fromAddress": "from address", 
     "headerFooterReplacements": [ 
      { 
      "placeholder": "##HEADER_PHONENUMBER##", 
      "value": "0000 000 000" 
      }, 
      { 
      "placeholder": "##HTML_TITLE##", 
      "value": "Title" 
      }, 
      { 
      "placeholder": "##EMAIL_HEADING##", 
      "value": "Heading" 
      } 
     ] 
     } 
    ] 
    } 
] 

我試圖用文本框更新的部分是

"emailTypeName": "EmailName", 
    "emailTypeId": 15, 
    "subject": "subject", 
    "fromAddress": "from address", 

但做編輯時,還需要使用客戶端來保存信息,以正確的客戶端和正確的模板ID和電子郵件類型ID作爲鍵。

  GetObjectRequest requestconfig = new GetObjectRequest(); 
      requestconfig.BucketName = "bucket"; 
      requestconfig.Key = "key"; 

      GetObjectResponse response = s3Client.GetObject(requestconfig); 

      using (StreamReader r = new StreamReader(response.ResponseStream)) 
      { 

       string json = r.ReadToEnd(); 
       dynamic jsonObj = JsonConvert.DeserializeObject(json); 
       if ((int)emailTempConfig.emailTypeId == templateID) 
       { 

        jsonObj["EmailTemplateConfig"][0]["EmailsTypeName"] = EmailNameText.Text; 
        output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented); 

       } 


      } 


      PutObjectRequest requestConfig = new PutObjectRequest(); 
      requestConfig.BucketName = "bucket"; 
      requestConfig.Key = "key"; 
      requestConfig.ContentType = "application/json"; 
      requestConfig.ContentBody = output; 

      s3Client.PutObject(requestConfig); 

這就是我所在的代碼。

回答

0

經過數小時的掙扎,我終於自己解決了,這裏是我用於任何需要它的人的參考的代碼。

最後它非常簡單我只需要特別挑選出我想要的電子郵件配置文件,然後專門挑出配置文件中的模板,並通過指定文本框的值來更新文本值。

var Emailcon = EmailConfigs.FirstOrDefault(key => key.EmailId == client); 
var Emailtemp = EmailConfigs.EmailTemplateConfigs.FirstOrDefault(key => (int)key.EmailTypeId == templateID); 

EMAILtemp.EmailTypeName = EmailNameText.Text; 
EMAILtemp.Subject = SubjectText.Text; 
EMAILtemp.FromAddress = FromAddressText.Text; 
Emailtemp.AdminToAddress = AdminToAddressText.Text; 
Emailtemp.SmsContentTemplateText = SmsContentText.Text; 

output = JsonConvert.SerializeObject(EmailComms, Formatting.Indented); 

反序列化 - >挑選出具體位置 - >重新序列化

真的很簡單。