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

2つのことのデータを取得したい。

  1. アドレス.モデル
  2. address.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 に答える