C# - 如何对从对象到对象数组不同的JSON数据进行反序列化?

时间:2017-04-10 17:10:42

标签: c# json nested json-deserialization

我得到的Json响应包含到公共交通目的地之间的旅行信息。

如果没有直接传输,包含信息的Json元素(" Leg")将是一个对象。但是,如果没有直接传输,(" Leg")元素将变为包含两个或多个具有不同属性的对象的数组。

当我尝试使用API​​模式中的自动生成的类(粘贴为特殊JSON)反序列化Json响应时,反序列化仅返回空值。

使用实际的Json响应生成C#类将(" Leg")元素设置为公共对象,没有属性的getter或setter。这样反序列化将起作用,但我无法将(" Leg")元素转换为它的属性。或者它可能是可能的,但我不知道如何。

这是Json响应

{
  "TripList": {
    "noNamespaceSchemaLocation": "http://api.xsd",
    "servertime": "03:10",
    "serverdate": "2017-04-11",
    "Trip": [
      {
        "Leg": {
          "name": "Spårvagn 6",
          "sname": "6",
          "type": "TRAM",
          "id": "xxx5014500604002",
          "direction": "Länsmansgården",
          "fgColor": "#fa8719",
          "bgColor": "#00394d",
          "stroke": "Solid",
          "Origin": {
            "name": "Centralstationen, Göteborg",
            "type": "ST",
            "id": "xxx2014001950004",
            "routeIdx": "1",
            "time": "04:05",
            "date": "2017-04-11",
            "track": "D",
            "rtTime": "04:05",
            "rtDate": "2017-04-11",
            "$": "\n"
          },
          "Destination": {
            "name": "Vågmästareplatsen, Göteborg",
            "type": "ST",
            "id": "xxx2014007520002",
            "routeIdx": "5",
            "time": "04:11",
            "date": "2017-04-11",
            "track": "B",
            "rtTime": "04:11",
            "rtDate": "2017-04-11",
            "$": "\n"
          },
          "JourneyDetailRef": { "ref": "xx}
        }
      },
      {
        "Leg": [
          {
            "name": "Gå",
            "type": "WALK",
            "Origin": {
              "name": "Göteborg C, Göteborg",
              "type": "ST",
              "id": "xxx2014008000015",
              "time": "04:42",
              "date": "2017-04-11",
              "$": "\n"
            },
            "Destination": {
              "name": "Nordstan, Göteborg",
              "type": "ST",
              "id": "xxx2014004945002",
              "time": "04:47",
              "date": "2017-04-11",
              "track": "B",
              "$": "\n"
            }
          },
          {
            "name": "Spårvagn 6",
            "sname": "6",
            "type": "TRAM",
            "id": "xxx5014500604012",
            "direction": "Länsmansgården",
            "fgColor": "#fa8719",
            "bgColor": "#00394d",
            "stroke": "Solid",
            "Origin": {
              "name": "Nordstan, Göteborg",
              "type": "ST",
              "id": "xxx2014004945002",
              "routeIdx": "11",
              "time": "04:47",
              "date": "2017-04-11",
              "track": "B",
              "rtTime": "04:47",
              "rtDate": "2017-04-11",
              "$": "\n"
            },
            "Destination": {
              "name": "Vågmästareplatsen, Göteborg",
              "type": "ST",
              "id": "xxx2014007520002",
              "routeIdx": "14",
              "time": "04:52",
              "date": "2017-04-11",
              "track": "B",
              "rtTime": "04:52",
              "rtDate": "2017-04-11",
              "$": "\n"
            },
            "JourneyDetailRef": { "ref": "xx" }
          }
        ]
      },
      {
        "Leg": [
          {
            "name": "Gå",
            "type": "WALK",
            "Origin": {
              "name": "Göteborg C, Göteborg",
              "type": "ST",
              "id": "xxx2014008000015",
              "time": "04:58",
              "date": "2017-04-11",
              "$": "\n"
            },
            "Destination": {
              "name": "Nordstan, Göteborg",
              "type": "ST",
              "id": "xxx2014004945002",
              "time": "05:03",
              "date": "2017-04-11",
              "track": "B",
              "$": "\n"
            }
          },
          {
            "name": "Spårvagn 6",
            "sname": "6",
            "type": "TRAM",
            "id": "xxx5014500604024",
            "direction": "Länsmansgården",
            "fgColor": "#fa8719",
            "bgColor": "#00394d",
            "stroke": "Solid",
            "Origin": {
              "name": "Nordstan, Göteborg",
              "type": "ST",
              "id": "xxx2014004945002",
              "routeIdx": "11",
              "time": "05:03",
              "date": "2017-04-11",
              "track": "B",
              "rtTime": "05:03",
              "rtDate": "2017-04-11",
              "$": "\n"
            },
            "Destination": {
              "name": "Vågmästareplatsen, Göteborg",
              "type": "ST",
              "id": "xxx2014007520002",
              "routeIdx": "14",
              "time": "05:08",
              "date": "2017-04-11",
              "track": "B",
              "rtTime": "05:08",
              "rtDate": "2017-04-11",
              "$": "\n"
            },
            "JourneyDetailRef": { "ref": "xx" }
          }
        ]
      }
    ]
  }
}

如上面的Json响应中所示," Leg"元素各不相同有时它是阵列,有时不是。在这个响应中,Leg的第一次出现似乎是一个对象,下面的两个Leg出现似乎是对象的数组,其中对象的属性是不同的,无论它是一个"步行路径"或者#34;电车路线"。这会随着每个响应而变化。 现在这些是使用acutal Json-response

生成的类
public class Rootobject
{
    public Triplist TripList { get; set; }
}

public class Triplist
{
    public string noNamespaceSchemaLocation { get; set; }
    public string servertime { get; set; }
    public string serverdate { get; set; }
    public Trip[] Trip { get; set; }
}

public class Trip
{
    public object Leg { get; set; }
}

我希望以一种方式对响应进行反序列化,我可以从#34; Leg"中获取数据,放入C#对象并将它们传递到视图中。

我不确定我是否能够以正确的方式解释问题,但我不知道如何处理这个问题。

我真的需要一些指导!

修改

我提出了一个解决方案,也许是一个丑陋的解决方案,至少它按照我想要的方式运行。我创建了一个名为" Leg"的单独模型,然后我只是用于循环Json响应的Leg部分,检查对象是否是数组。

附加模型类:

public class Leg
{
    public string name { get; set; }
    public string sname { get; set; }
    public string type { get; set; }
    public string id { get; set; }
    public string direction { get; set; }
    public string fgColor { get; set; }
    public string bgColor { get; set; }
    public string stroke { get; set; }
    public string accessibility { get; set; }
    public Origin Origin { get; set; }
    public Destination Destination { get; set; }
    public Journeydetailref JourneyDetailRef { get; set; }
}

public class Origin
{
    public string name { get; set; }
    public string type { get; set; }
    public string id { get; set; }
    public string routeIdx { get; set; }
    public string time { get; set; }
    public string date { get; set; }
    public string track { get; set; }
    public string rtTime { get; set; }
    public string rtDate { get; set; }
    public string _ { get; set; }
}

public class Destination
{
    public string name { get; set; }
    public string type { get; set; }
    public string id { get; set; }
    public string routeIdx { get; set; }
    public string time { get; set; }
    public string date { get; set; }
    public string track { get; set; }
    public string rtTime { get; set; }
    public string rtDate { get; set; }
    public string _ { get; set; }
}

public class Journeydetailref
{
    public string _ref { get; set; }
}

方法

// Deserialising the full Json-response.
        var json = JsonConvert.DeserializeObject<TripModel>(jsonString);

        result = json;

        // Nested list of "Leg". Will be populated with both direct-routes and routes with connections.
        List<List<Leg>> completeLegList = new List<List<Leg>>();
        List<Leg> root = new List<Leg>();          

        // Just to make a variable easier to work with           
        string legTrimmed;        

        for (int i = 0; i < json.TripList.Trip.Length; i++)
        {
            // Assigning the variable
            legTrimmed = json.TripList.Trip[i].Leg.ToString();
            // Sorting out the trips containing 1 or more transerfs (looking for the opening array character.)

            if (legTrimmed.Contains("["))
            {                                                    
                // Replacing whitespace and linebreaks
                legTrimmed.Replace(" ", string.Empty);
                legTrimmed.Replace("\r\n", string.Empty);
                // Deserializing just the Leg-data from the master Json-response. Putting the results in an list.
                var legObjectMultiple = JsonConvert.DeserializeObject<List<Leg>>(legTrimmed);
                // Adding the list (with multiple connections) to the main Leg-list.
                completeLegList.Add(legObjectMultiple);

            }
            // Trips with direct connection below.
            else
            {                 
                legTrimmed = json.TripList.Trip[i].Leg.ToString();
                legTrimmed.Replace(" ", string.Empty);
                legTrimmed.Replace("\r\n", string.Empty);
                Leg legObjectSingle = JsonConvert.DeserializeObject<Leg>(legTrimmed);
                var legListSingle = new List<Leg>();
                legListSingle.Add(legObjectSingle);                 
                completeLegList.Add(legListSingle);
            }
        }

        return completeLegList;

由于 最好 Ĵ

0 个答案:

没有答案