2017-09-29 120 views
-1

我試圖JSON響應對象轉換爲字符串,但我有一個問題 這是我使用轉換爲AC#NuGet包的文檔串不能轉換成字符串

https://craftyclicks.co.uk/docs/postcode-lookup/#full-address-rapidaddress

更具體地說,這與返回任何錯誤有關。

https://craftyclicks.co.uk/docs/postcode-lookup/#debugging

public void GetRapidAddressByPostCode(string mPostCode) 
    { 

     mApiKey = ConfigurationManager.AppSettings["CraftyClicksApiKey"]; 
     string urlToApi = ConfigurationManager.AppSettings["CraftyClicksApiUrl"]; 


     if (!String.IsNullOrEmpty(urlToApi)) 
     { 
      url = String.Format(urlToApi + "?postcode={0}&response=data_formatted&key={1}", 
       mPostCode, mApiKey); 

     } 
     else 
     { 
      url = String.Format("http://pcls1.craftyclicks.co.uk/json/rapidaddress?postcode={0}&response=data_formatted&key={1}", 
      mPostCode, mApiKey); 
     } 



     //Complete XML HTTP Request 
     WebRequest request = WebRequest.Create(url); 
     //Complete XML HTTP Response 
     WebResponse response = request.GetResponse(); 

     //Declare and set a stream reader to read the returned XML 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 

     string json = reader.ReadToEnd(); 
     // Get the requests json object and convert it to in memory dynamic 
     // Note: that you are able to convert to a specific object if required. 
     var jsonResponseObject = JsonConvert.DeserializeObject<dynamic>(reader.ReadToEnd()); 
     // check that there are delivery points 
     if (jsonResponseObject !=null || jsonResponseObject.delivery_points !=null) 
     { 

      //If the node list contains address nodes then move on. 
      int i = 0; 

      foreach (var node in jsonResponseObject.delivery_points) 
      { 
       ClsAddress address = new ClsAddress() 
       { 
        AddressID = i, 
        AddressLine1 = node.line_1, 
        AddressLine2 = node.line_2, 

        County = jsonResponseObject.postal_county, 
        PostCode = jsonResponseObject.postcode, 
        Town = jsonResponseObject.town 



       }; 

       addressList.Add(address); 
       i++; 
      } 
     } 
     else 
     { 




    foreach (var node in jsonResponseObject) 
      { 
       // Get the details of the error message and return it the user. 
       switch ((string)node.Value) 
       { 
        case "0001": 
         mStatus = "Post Code not found"; 
         break; 
        case "0002": 
         mStatus = "Invalid Post Code format"; 
         break; 
        case "7001": 
         mStatus = "Demo limit exceeded"; 
         break; 
        case "8001": 
         mStatus = "Invalid or no access token"; 
         break; 
        case "8003": 
         mStatus = "Account credit allowance exceeded"; 
         break; 
        case "8004": 
         mStatus = "Access denied due to access rules"; 
         break; 
        case "8005": 
         mStatus = "Access denied, account suspended"; 
         break; 
        case "9001": 
         mStatus = "Internal server error"; 
         break; 
        default: 
         mStatus = (string)node.Value; 
         break; 
       } 
      } 




     } 
    } 

但我得到的問題是,它說,它無法在switch語句轉換數組的字符串。我認爲這個問題是,當我訪問的投遞點,當它不存在,我認爲我的空檢查

編輯出示有效的JSON

當A郵編找不到下面將會返回JSON

{ 「ERROR_CODE」: 「0001」, 「ERROR_MSG」: 「未找到數據的請求 郵政編碼。」}

然而,當一個有效的屬性發現郵政編碼會返回以下內容。

{ 「delivery_points」:[{ 「ORGANISATION_NAME」: 「」, 「部門名稱」: 「」, 「LINE_1」: 「1 亨廷戴爾 CRESCENT」, 「LINE_2」: 「」, 「udprn」: 「03488593」,「dps」:「1A」},{「organisation_name」:「」,「department_name」:「」,「line_1」:「3 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」 :「03488604」,「dps」:「1B」},{「organisation_name」:「」「,」department_name「:」「,」line_1「:」5 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn 「:」03488606「,」dps「:」1D「},{」organisation_name「:」「,」department_name「:」「,」line_1「:」7 HUNTINGDALE CRESCENT「,」line_2「:」「, udprn 「:」 03488608" , 「DPS」: 「1E」},{ 「ORGANISATION_NAME」: 「」, 「部門名稱」: 「」, 「LINE_1」:「HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488610「,」dps「:」1F「},{」organisation_name「:」「,」department_name「:」「,」line_1「:」 11 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488595「,」dps「:」1G「},{」organisation_name「:」「,」department_name「:」「,」line_1「: 「15 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488598」,「dps」:「1H」},{「organisation_name」:「」,「department_name」:「」,「line_1」 :「17 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488600」,「dps」:「1J」},{「organisation_name」:「」,「department_name」:「」,「line_1 「:」2 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488603「,」dps「:」1L「},{」organisation_name「:」「,」department_name「:」「, line_1「:」4 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488605「,」dps「:」1N「},{」organisation_name「:」「,」department_name「:」「,」line_1「:」6 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488607「,」dps「:」1P「},{」organisation_name「:」「,」department_name「:」「,」line_1「:」HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488609「,」dps「:」1Q「},{」organisation_name「:」「,」department_name「:」「,」line_1「:」 10 HUNTINGDALE CRESCENT「,」line_2「:」「,」udprn「:」03488594「,」dps「:」1R「},{」organisation_name「:」「,」department_name「:」「,」line_1「: 「12 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488596」,「dps」:「1S」},{「organisation_name」:「」,「department_name」:「」,「line_1」 :「14 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488597」,「dps」:「1T」},{「org 「,」「,」「,」「,」「,」「,」「,」 「organisation_name」:「」,「department_name」:「」,「line_1」:「18 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488601」,「dps」:「1W」}, {「organisation_name」:「」,「department_name」:「」,「line_1」:「19 HUNTINGDALE CRESCENT」,「line_2」:「」,「udprn」:「03488602」,「dps」:「1X」} ], 「delivery_point_count」:18, 「postal_county」: 「縣 ANTRIM」, 「traditional_county」: 「縣 ANTRIM」, 「鎮」: 「巴利克萊爾」, 「郵政編碼」: 「BT39 9YY」}

系統.ArgumentException:'無法將數組轉換爲字符串。'

所討論的JSON是如下

顯示我目前調試。

enter image description here

+0

發佈一個有效的JSON可以幫助您得到更好的答案 –

+0

@LB是有效的JSON如何它不在我的評論 – david

+0

:)))複製你json從問題和粘貼到http://json2csharp.com/或https://jsonlint.com/ –

回答

1

我認爲有在你的代碼中的問題由下面的檢查引起的:

if (jsonResponseObject !=null || jsonResponseObject.delivery_points !=null) 

由於此檢查將返回true J​​SON響應是否返回錯誤的對象或預期包含delivery_points的對象。

你的代碼應該是這樣的:

if (jsonResponseObject !=null) 
{ 
    if(jsonResponseObject.delivery_points !=null) 
    { 
      //If the node list contains address nodes then move on. 
      int i = 0; 

      foreach (var node in jsonResponseObject.delivery_points) 
      { 
       ClsAddress address = new ClsAddress() 
       { 
        AddressID = i, 
        AddressLine1 = node.line_1, 
        AddressLine2 = node.line_2, 

        County = jsonResponseObject.postal_county, 
        PostCode = jsonResponseObject.postcode, 
        Town = jsonResponseObject.town 
       }; 

       addressList.Add(address); 
       i++; 
      } 
     } 
     else 
     { 
      foreach (var node in jsonResponseObject) 
      { 
       // Get the details of the error message and return it the user. 
       switch ((string)node.Value) 
       { 
        case "0001": 
         mStatus = "Post Code not found"; 
         break; 
        case "0002": 
         mStatus = "Invalid Post Code format"; 
         break; 
        case "7001": 
         mStatus = "Demo limit exceeded"; 
         break; 
        case "8001": 
         mStatus = "Invalid or no access token"; 
         break; 
        case "8003": 
         mStatus = "Account credit allowance exceeded"; 
         break; 
        case "8004": 
         mStatus = "Access denied due to access rules"; 
         break; 
        case "8005": 
         mStatus = "Access denied, account suspended"; 
         break; 
        case "9001": 
         mStatus = "Internal server error"; 
         break; 
        default: 
         mStatus = (string)node.Value; 
         break; 
       } 
      } 
     } 
    } 
+0

這似乎是它,但因爲我填充狀態,因爲字符串是正確的方法來做到這一點沒有更好的方式拋出錯誤返回 – david