2015-01-13 2 views
1

JSON 역 직렬화에 대한 내 코드의 문제점을 찾아 내고 많은 게시물을 읽지 만 정확한 해결책을 찾을 수 없었습니다. JSON은 클라이언트에 의해 보내지고 때로는 성공하고 때로는 오류로 인해 실패합니다. " 'System.String'유형에 대해 매개 변수없는 생성자가 정의되지 않았습니다." 이 줄의 : RootObject myData = new JavaScriptSerializer(). Deserialize (json);JSON 역 직렬화에서 매개 변수가없는 생성자가 정의되지 않았습니다.

C#을

DataTable dt = new DataTable(); 
dt.Columns.Add("isbn", typeof(string)); 
dt.Columns.Add("price", typeof(string)); 
dt.Columns.Add("uri", typeof(string)); 

using (var WebClient = new WebClient()) 
{ 
    string json = WebClient.DownloadString("http://www.pricetree.com/internal/test.txt"); 
    RootObject myData = new JavaScriptSerializer().Deserialize<RootObject>(json); 

    foreach (var item in myData.results.collection1) 
    { 
     dt.Rows.Add(item.url, item.price, item.uri); 
    } 
} 

클래스

public class Collection1 
{ 
    public string price { get; set; } 
    public string uri { get; set; } 
    public string url { get; set; } 
} 

public class Results 
{ 
    public List<Collection1> collection1 { get; set; } 
} 

public class RootObject 
{ 
    public string name { get; set; } 
    public int count { get; set; } 
    public string lastrunstatus { get; set; } 
    public string lastsuccess { get; set; } 
    public Results results { get; set; } 
} 

이미 아래 스레드 아래에서 몇 가지 대답을 볼 수 있지만 작동하지 않았다. 아무도 나에게 맞는 솔루션을 제안 할 수 있습니다.

No parameterless constructor defined for type of 'System.String' during JSON deserialization

Screenshot for error

+0

조금 넣어주세요 게시하기 전에 코드를 포맷하는 데 더 많은 시간이 소요됩니다. 오래 걸리지는 않지만 가독성에 큰 차이가 있습니다. –

+0

확실한 @ 존 키켓. 이제 좋아 보인다. – Vicky

+1

이상적으로는 실패한 JSON 조각을 분리하여 질문에 포함시키는 것이 가장 좋습니다 (코드를 간단하게 작성한 후). 문제를 보여주는 짧지 만 완전한 프로그램을 만들어보십시오. –

답변

3

당신이이 클래스를 사용하는 경우가 작동합니다

public class Collection1 
{ 
    public string price { get; set; } 
    public object uri { get; set; } 
    public string url { get; set; } 
} 

public class Results 
{ 
    public List<Collection1> collection1 { get; set; } 
} 

public class RootObject 
{ 
    public string name { get; set; } 
    public int count { get; set; } 
    public string frequency { get; set; } 
    public int version { get; set; } 
    public bool newdata { get; set; } 
    public string lastrunstatus { get; set; } 
    public string lastsuccess { get; set; } 
    public string thisversionstatus { get; set; } 
    public string thisversionrun { get; set; } 
    public Results results { get; set; } 
} 

JSON 자동 마술에서의 C# 클래스를 구축 할 예정이 사이트 : http://json2csharp.com/

+0

그 사이트는 살인자, 나에게 시간, 감사의 무리를 저장! –

관련 문제