2013-11-15 3 views
0

많은 노력을했지만이 json을 구문 분석 할 수 없습니다.C#에서 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. addresses.model 내가 모델로부터 데이터를 얻을이 시도했습니다

  • 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-information-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

    고마워 :) :) –

    관련 문제