猿问

需要帮助反序列化从 API 返回的 JSON 对象

我有以下 JSON 输出:


{{

  "$type": "Asi.Soa.Core.DataContracts.PagedResult`1[[Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts]], Asi.Contracts",

  "Items": {

    "$type": "System.Collections.Generic.List`1[[Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts]], mscorlib",

    "$values": [

      {

        "$type": "Asi.Soa.Core.DataContracts.GenericEntityData, Asi.Contracts",

        "EntityTypeName": "14",

        "Properties": {

          "$type": "Asi.Soa.Core.DataContracts.GenericPropertyDataCollection, Asi.Contracts",

          "$values": [

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "ResultRow",

              "Value": "1"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "Work Phone",

              "Value": "(782) 438-7600"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "Email",

              "Value": "agsaz@rmax.net"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "Full Name",

              "Value": "Agazny"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "iMISId",

              "Value": "eg1"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "Preferred Phone",

              "Value": "780"

            },

            {

              "$type": "Asi.Soa.Core.DataContracts.GenericPropertyData, Asi.Contracts",

              "Name": "Organization",

              "Value": "Re"

            }

          ]

        }

      },

     

我正在尝试将此响应反序列化为可用的 c# 对象,并将其发布到接受完全不同格式的 JSON 的不同 API。


大话西游666
浏览 167回答 1
1回答

呼唤远方

您提供的代码中存在错误。当它是一个对象时,您正试图将其Properties转换为 a JArray。如果确实需要Properties对象中的数组,请执行以下操作:JArray arr = (JArray)result["Items"]["$values"][0]["Properties"]["$values"];否则,如果您正在寻找Properties对象,那么您应该这样做:JObject obj = (JObject)result["Items"]["$values"][0]["Properties"];最后,要重建Customer实例列表,您可以将以下逻辑与静态方法一起使用:var customersJson = (JArray)result["Items"]["$values"];var customers = new List<Customer>();foreach (JObject o in customersJson){&nbsp; &nbsp; &nbsp;var customerJson = (JArray) o["Properties"]["$values"];&nbsp; &nbsp; &nbsp;customers.Add(BuildCustomer(customerJson));}[...]private static Customer BuildCustomer(JArray a){&nbsp; &nbsp; return new Customer&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ResultRow = GetValue(a, "ResultRow"),&nbsp; &nbsp; &nbsp; &nbsp; WorkPhone = GetValue(a, "Work Phone"),&nbsp; &nbsp; &nbsp; &nbsp; Email = GetValue(a, "Email"),&nbsp; &nbsp; &nbsp; &nbsp; FullName = GetValue(a, "Full Name"),&nbsp; &nbsp; &nbsp; &nbsp; iMISId = GetValue(a, "iMISId"),&nbsp; &nbsp; &nbsp; &nbsp; PreferredPhone = GetValue(a, "Preferred Phone"),&nbsp; &nbsp; &nbsp; &nbsp; Organization = GetValue(a, "Organization")&nbsp; &nbsp; &nbsp;};}private static string GetValue(JArray array, string name){&nbsp; &nbsp; JToken obj = array.FirstOrDefault(x => (string) x["Name"] == name);&nbsp; &nbsp; if (obj == null)&nbsp; &nbsp; &nbsp; &nbsp; return string.Empty;&nbsp; &nbsp; return (string) obj["Value"];}
随时随地看视频慕课网APP
我要回答