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
조금 넣어주세요 게시하기 전에 코드를 포맷하는 데 더 많은 시간이 소요됩니다. 오래 걸리지는 않지만 가독성에 큰 차이가 있습니다. –
확실한 @ 존 키켓. 이제 좋아 보인다. – Vicky
이상적으로는 실패한 JSON 조각을 분리하여 질문에 포함시키는 것이 가장 좋습니다 (코드를 간단하게 작성한 후). 문제를 보여주는 짧지 만 완전한 프로그램을 만들어보십시오. –