2012-04-01 2 views
0

저는 json으로 작업하는 데있어서 새로운입니다. 기존의 json 데이터 구조로 작업 중이며 데이터를 출력하려고하지만 기존 데이터 구조의 일부가 저를 곤혹스럽게합니다. - 또 다른 중첩 된 JSON 문자열 나를는 정보 데이터를하다 난처한 상황에 빠진C# Deserialize 중첩 된 json

{"supplier": 
    { 
    "supplierid":3590, 
    "code":"ENCLES", 
    "name":"Les Miserables", 
    "analyses":[], 
    "amenities":[], 
    "info": 
     "{\"Supplier\": 
      { 
      \"Name\":\"Les Miserables\", 
      \"LastUpdate\":\"2011-11-01T22:16:06Z\", 
      \"Address3\":\"London\", 
      \"Address2\":\"51 Shaftesbury Avenue\", 
      \"PostCode\":\"W1D 6BA\", 
      \"Address1\":\"Queen's Theatre\", 
      \"Address4\":\"\", 
      \"Address5\":\"\", 
      \"SupplierId\":3590, 
      \"SupplierCode\":\"ENCLES\" 
      } 
     }", 
     ... 
     } 

비트 : 다음

내 JSON 데이터입니다.

내 클래스입니다 :

public class TheatreListing 
{ 
    public supplier supplier; 
} 

public class supplier 
{ 
    public int? supplierid { get; set; } 
    public string code { get; set; } 
    public string name { get; set; } 
    public listingInfo info { get; set; } 
} 


public class listingInfo 
{ 
    public Address Supplier { get; set; } 

} 

public class Address 
{ 
    public string Address1 { get; set; } 
    public string Address2 { get; set; } 
    public string Address3 { get; set; } 
    public string Address4 { get; set; } 
    public string Address5 { get; set; } 
    public string PostCode { get; set; } 
} 

데이터를 시도하고 액세스 할 수 내 코드는 다음과 같습니다

TheatreListing tl = Json.Decode<TheatreListing>(json); 
StringBuilder sbb = new StringBuilder(); 
sbb.Append("Name = " + tl.supplier.name.ToString()); 
sbb.Append("<br />Supplier ID = " + tl.supplier.supplierid.ToString()); 
sbb.Append("<br />Code = " + tl.supplier.code.ToString()); 
sbb.Append("<br />Address = " + tl.supplier.info.Supplier.Address2.ToString()); 
litOutput.Text += sbb.ToString(); 

내가 무엇입니까 오류 메시지는 다음과 같습니다

Cannot convert object of type 'System.String' to type 'listingInfo' 

수있는 사람하시기 바랍니다 여기 내 방식의 오류에 대해 안내해 주시겠습니까?

건배

나이젤

+0

'문자열'에 내부 자료를 비 직렬화 한 다음 2 단계에서 올바른 유형으로 직렬화 해제하는 것이 좋습니다. 사용자 정의 형식 변환기를 등록 할 수는 있지만 ... 이상적으로는 못생긴 방식으로 중첩되지는 않습니다. - /) –

+0

"info": "{\"Supplier \ ": ..."정보 "가 아닙니다 : {\"공급 업체 \ ": (앞 인용 부호없이 중괄호)와 끝괄 중괄호에 대해 동일한 것? – mho

답변

0

문제는 내가 TheatreListing로의 전환은 현재 JSON 실패 생각 라인

TheatreListing tl = Json.Decode<TheatreListing>(json); 

안에 있습니다.

왜 JavascriptSerializer를 사용하여 작동하는지 확인하십시오.

JavaScriptSerializer js = new JavaScriptSerializer(); 
TheatreListing tree = js.Deserialize <TheatreListing>(json); 
3

나는 몇 가지보고 추천 :

1) 기존의 JSON

2) JSON을 역 직렬화하는 json.net를 사용에서 C#을 클래스를 생성 json2csharp를 사용는 챔피언처럼 작동 !