2013-11-15 123 views
0

我試了很多,但我不能解析這個JSON。無法解析JSON在C#

{ 
"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. addresses.model
  2. addresses.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] 

幫助我..

+0

你可能想看到這個先生:http://stackoverflow.com/questions/19307752/deserializing-polymorphic-json-classes-without-type-in​​formation-using-json-net –

回答

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

,只需將它們添加到上面的相應類中即可。

+0

謝謝:)它工作:) –