# Visual Studio 使用 JSON (備註:*UOF*是產品名稱) <br/> ## 步驟說明 * 安裝JSON,並將Newtonsoft.Json.dll加入至參考。 * 加入using,便可使用JSON * using Newtonsoft.Json; * SeserializeObject(): 序列化成JSON。 * 參考:https://dotblogs.com.tw/shadow/2012/08/16/74099 <br/> ## (重要!!)檢查專案有無JSON套件 因為我們是複製UOF專案到本機做修改,因此要先檢查專案本身,看有沒有JSON套件 因UOF本身其實已有JSON套件,但Visual Studio不會顯示,我以為沒有,所以裝了較新版本的JSON,導致版本衝突,程式異常 ``` Exception information: Exception type: HttpException Exception message: 無法載入檔案或組件 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其相依性的其中之一。 找到的組件資訊清單定義與組件參考不符。 (發生例外狀況於 HRESULT: 0x80131040) 於 System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) 於 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) 於 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) 於 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) 於 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) ``` ### 檢查步驟1:看bin底下有沒有Json的dll ![](https://i.imgur.com/whgvjgG.png) <br/> ### 檢查步驟2:Web.config有參考到Json ![](https://i.imgur.com/CFeawld.png) <br/>如果兩個都有,表示專案已有Json,可跳過安裝,直接寫程式,請看「程式撰寫」 若沒有則往下繼續看「安裝JSON套件」 --- ## 安裝JSON套件 專案 → 管理NuGet套件 ![](https://i.imgur.com/bdDb3ET.png) <br/> 搜尋「Newtonsoft.Json」,並下載 ![](https://i.imgur.com/TjVT96x.png) <br/> 完成後,可在bin底下找到 Newtonsoft.Json.dll ![](https://i.imgur.com/2La1ArD.png) <br/> --- ## 程式撰寫 增加using ``` using Newtonsoft.Json; using Newtonsoft.Json.Linq; ``` 可使用JSON物件 ``` JObject jObject = new JObject(); JArray jArray = new JArray(); ``` <br/> ### 將資料轉組成JSON格式 ``` jObject.Add("creator", "1400295"); jObject.Add("place", "地點test"); ``` 最後序列化 ``` JsonConvert.SerializeObject(jObject).ToString() ``` <br/> ### 將JSON String轉換成JSON物件並取值 轉成JSON物件 ``` JObject jObject = (JObject)JsonConvert.DeserializeObject(result); ``` JObject 取值 ``` string errCode = jObject.GetValue("errCde"); ``` JArray 取值 ``` JArray degreeArr = JArray.Parse(jObject.GetValue("responses").ToString()) ; foreach (JObject json in degreeArr) { string pmLevel = json.GetValue("Degree").ToString(); string digitalLesson = json.GetValue("DigitalLesson").ToString(); string slackLesson = json.GetValue("SlackLesson").ToString(); } ``` <br/> ## 範例:將明細資料組成JSON格式,並顯示於txtFieldValue.Text ``` protected void btnTest_Click(object sender, EventArgs e) { txtFieldValueTest.Text = ""; JObject jObject = new JObject(); JArray jArray = new JArray(); JObject objectCRM = new JObject(); objectCRM.Add("creator", "1400295"); objectCRM.Add("type", "convert"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(txtFieldValue.Text); foreach (XmlNode node in xmlDoc.SelectNodes("./FieldValue/Item")) { JObject jsonItem = new JObject(); jsonItem.Add("reason", HttpUtility.UrlEncode(node.Attributes["reason"].Value, Encoding.UTF8)); jsonItem.Add("travelTool", HttpUtility.UrlEncode(node.Attributes["travelTool"].Value, Encoding.UTF8)); jsonItem.Add("travelCost", HttpUtility.UrlEncode(node.Attributes["travelCost"].Value, Encoding.UTF8)); jArray.Add(jsonItem); } jObject.Add("auth", objectCRM); jObject.Add("meeting", jArray); txtFieldValueTest.Text += JsonConvert.SerializeObject(jObject).ToString(); } ``` 結果: ``` { "auth": { "creator": "1400295", "type": "convert" }, "meeting": [ { "reason": "33", "travelTool": "%e8%a8%88%e7%a8%8b%e8%bb%8a", "travelCost": "120" }, { "reason": "cccc", "travelTool": "%e7%a7%81%e8%bb%8a%e5%85%ac%e7%94%a8", "travelCost": "300" }, { "reason": "%e5%85%ac%e5%87%ba%e5%8e%9f%e5%9b%a0111", "travelTool": "%e5%a4%a7%e7%9c%be%e4%ba%a4%e9%80%9a%e5%b7%a5%e5%85%b7", "travelCost": "25" }, { "reason": "%e5%85%ac%e5%87%ba%e5%8e%9f%e5%9b%a0222", "travelTool": "%e7%84%a1", "travelCost": "0" } ] } ``` <br/> ## 範例:從外部系統取得JSON字串,將其轉成JSON物件,並取值 ``` #從BMS取得的字串 { "errMsg":"", "responses":[ { "userId":"1400295", "Degree":"0", "DigitalLesson":"N", "SlackLesson":"N" } ], "errCde":0 } ``` ``` #轉成JSON物件 (result是JSON字串) JObject jObject = (JObject)JsonConvert.DeserializeObject(result); if (0 == Convert.ToInt32(jObject.GetValue("errCde"))) { JArray degreeArr = JArray.Parse(jObject.GetValue("responses").ToString()); foreach (JObject json in degreeArr) { string pmLevel = json.GetValue("Degree").ToString(); string digitalLesson = json.GetValue("DigitalLesson").ToString(); string slackLesson = json.GetValue("SlackLesson").ToString(); } } ```