响应对象中的字符串无法转换为字符串

时间:2017-09-29 21:30:22

标签: c# json

我正在尝试将json响应对象转换为字符串,但我有一个问题 这是我用来转换为c#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

如果找不到邮政编码,将返回以下json

  

{&#34; error_code&#34;:&#34; 0001&#34;,&#34; error_msg&#34;:&#34;未找到所请求的数据   邮政编码&#34;}

但是,当找到有效的属性邮政编码时,它将返回以下内容。

  

{&#34; delivery_points&#34;:[{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34; &#34; LINE_1&#34;:&#34; 1   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488593&#34;&#34; DPS&#34 ;: &#34; 1A&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 3   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488604&#34;&#34; DPS&#34 ;: &#34; 1B&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 5   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488606&#34;&#34; DPS&#34 ;: &#34; 1D&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 7   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488608&#34;&#34; DPS&#34 ;: &#34; 1E&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 9   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488610&#34;&#34; DPS&#34 ;: &#34; 1F&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 11   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488595&#34;&#34; DPS&#34 ;: &#34; 1G&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 15   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488598&#34;&#34; DPS&#34 ;: &#34; 1 H&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 17   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488600&#34;&#34; DPS&#34 ;: &#34; 1J&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 2   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488603&#34;&#34; DPS&#34 ;: &#34; 1L&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 4   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488605&#34;&#34; DPS&#34 ;: &#34; 1N&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 6   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488607&#34;&#34; DPS&#34 ;: &#34; 1P&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 8   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488609&#34;&#34; DPS&#34 ;: &#34; 1Q&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 10   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488594&#34;&#34; DPS&#34 ;: &#34; 1R&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 12   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488596&#34;&#34; DPS&#34 ;: &#34; 1S&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 14   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488597&#34;&#34; DPS&#34 ;: &#34; 1T&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 16   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488599&#34;&#34; DPS&#34 ;: &#34; 1 H&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 18   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488601&#34;&#34; DPS&#34 ;: &#34; 1W&#34;},{&#34; ORGANISATION_NAME&#34;:&#34;&#34;&#34; DEPARTMENT_NAME&#34;:&#34;&#34;&#34 ; LINE_1&#34;:&#34; 19   亨廷戴尔   CRESCENT&#34;&#34; LINE_2&#34;:&#34;&#34;&#34; udprn&#34;:&#34; 03488602&#34;&#34; DPS&#34 ;: &#34; 1X&#34;}],&#34; delivery_point_count&#34;:18,&#34; postal_county&#34;:&#34;县   ANTRIM&#34;&#34; traditional_county&#34;:&#34;县   ANTRIM&#34;,&#34; town&#34;:&#34; BALLYCLARE&#34;,&#34; postcode&#34;:&#34; BT39 9YY&#34;}

     

System.ArgumentException:&#39;无法将数组转换为字符串。&#39;

有问题的json如下

目前显示我的调试。

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您的代码中存在由以下检查引起的问题:

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

由于JSON响应返回错误对象或包含delivery_points的预期对象,因此此检查将返回true。

您的代码应如下所示:

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;
                }
            }
        }
    }