0

我尝试了很多,但无法解析这个 json。

{
"ver": 1,
"data": {
"addresses": [
  {
    "model": "SPR-4600m-A",
    "serial": "2001751041",
    "address": {
      "ver": 2,
      "id": 18130,
      "nickname": "Birkenweg 10",
      "streeAddress": "Birkenweg 10",
      "city": "Negast",
      "state": "--",
      "postalcode": "18442",
      "graphOptConsumption": {
        "id": 1,
        "name": "Off",
        "optType": "Consumption",
        "i18nText": "Off",
        "i18nKey": "GraphOption_1"
      },
      "GraphOptProduction": {
        "id": 5,
        "name": "Actual for this home",
        "optType": "Production",
        "i18nText": "Actual for this home",
        "i18nKey": "GraphOption_5"
      },
      "SunriseInfo": {
        "SunPoweron": "2013-11-14T07:33:54+01:00",
        "SunPoweroff": "2013-11-14T16:09:51+01:00",
        "HoursOfSunlight": {
          "days": 0,
          "hours": 8,
          "minutes": 35,
          "seconds": 57
        },
        "SunPoweronDTO": {
          "DateTime": "\/Date(1384410834000)\/",
          "OffsetMinutes": 60
        },
        "SunPoweroffDTO": {
          "DateTime": "\/Date(1384441791000)\/",
          "OffsetMinutes": 60
        }
      },
      "TimeZone": {
        "id": 16,
        "TimeZoneName": "Central European",
        "GMTOffset": 1.00,
        "MStimezoneName": "Central European Standard Time",
        "i18nKey": "sptimezone_central_european"
      },
      "hasMeter": false,
      "demoGuid": "c0d43641-fa09-4b00-b7b5-6af94a643f59",
      "accountId": 1116,
      "countryCode": "DE",
      "latitude": 54.252070,
      "longitude": 13.029600,
      "addressType": "Residential",
      "communityId": null,
      "ctLocationType": "None",
      "isLeased": false,
      "isPGU": false,
      "CTScaleFactor": 0.000,
      "LifetimeProdAdjust": 0.00,
      "dealerAccess": {
        "allow": true,
        "ts": "2011-11-21T17:01:15+00:00",
        "userId": 48940
      },
      "locale": "de_DE",
      "DisplayTimezoneName": "Central European",
      "IsInDST": false,
      "addressSubType": "SMS 1.x - GW\/DL"
    },
    "allowRegistration": true
  }
]
}
}

我想获得两件事的数据。

  1. 地址.模型
  2. 地址.graphOptConsumption.optType

我试过这个从模型中获取数据。

var data = (JObject)JsonConvert.DeserializeObject(json["data"].ToString());
        var addressInfo = data["addresses"].Children()["model"].Values<string>();
        Label2.Text = addressInfo.ToString();  

但我得到这样的输出。

Newtonsoft.Json.Linq.Extensions+d__4`2[Newtonsoft.Json.Linq.JToken,System.String]

帮我..

4

1 回答 1

2

你可以试试这个:

var d = JsonConvert.DeserializeObject<Wrapper>(a);

foreach (var addrWrapper in d.Data.Addresses)
{
    var model = addrWrapper.Model;
    var optType = addrWrapper.Address.GraphOptConsumption.OptType;

    // do whatever you want here
}

你需要这些类:

public class Wrapper
{
    public Data Data { get; set; }
}

public class Data
{
    public List<AddressWrapper> Addresses { get; set; }
}

public class AddressWrapper
{
    public string Model { get; set; }
    public Address Address { get; set; }
}

public class Address 
{
    public GraphOptConsumption GraphOptConsumption { get; set; }
}

public class GraphOptConsumption
{
    public string OptType { get; set; }
}

如果您最终需要更多字段,只需将它们添加到上面的相应类中。

于 2013-11-15T05:35:15.247 回答