필요한 JSON 정보를 동적으로 선택하십시오
-
20-12-2019 - |
문제
나는 일부 JSON을 할당해야하지만 문제는 모든 JSON 객체의 모든 것이 똑같은 형식을 갖지 않는다는 것입니다. 다음은 내가 deserialize 해야하는 JSON의 예입니다.
[
{
"Player_Name": "Zlatan Ibrahimovic",
"Country": "Sweeden",
"Other_Informations": {
"Information1": [
]
},
},
{
"Player_Name": "Pavel Nedved",
"Country": "Czech Republic",
"Personal_Honours":
{
"Ballon_DOr": "One",
},
"Other_Informations": {
"Information1": [
]
},
},
{
"Player_Name": "Zinedine Zidane",
"Country": "Sweeden",
"Other_Informations": {
"Information1": [
{
"name": "example",
}
]
},
}
]
.
보시다시피 일부 필드는 예를 들어 "personal_honours"와 같은 개체에만 나타납니다. 이 클래스에 JSON을 삭제해야합니다.
public class PlayerData
{
public string Name { get; set; }
public string BallonDor {get; set; }
public string Information1{ get; set; }
public string Country{ get; set; }
}
.
이 방법을 사용하여 실제로 길고 내 앱을 차단합니다. (이 사상에서 나는 TextFile에서 오는 Json을 사용하지만 일반적으로 휴식을 취해야합니다.)
StreamReader reader = File.OpenText("TextFile1.txt");
List<PlayerData> DataList;
dynamic value= JsonConvert
.DeserializeObject<dynamic>(reader.ReadToEnd());
DataList = new List<PlayerData>();
foreach (dynamic data in value)
{
if (data.Personal_Honours == null)
{
if (data.Other_Informations.Information1 == null)
{
DataList.Add(new PlayerData
{
Name = data.Player_Name,
Country = data.Country,
});
}
else
{
DataList.Add(new PlayerData
{
Name = data.Player_Name,
Country = data.Country,
Information1 = data.Informations.Information1
});
}
}
else
{
if (data.Other_Informations.Information1 == null)
{
DataList.Add(new PlayerData
{
Name = data.Player_Name,
Country = data.Country,
BallonDor = data.Personal_Honours.Ballon_DOr
});
}
else
{
DataList.Add(new PlayerData
{
Name = data.Player_Name,
Country = data.Country,
BallonDor = data.Personal_Honours.Ballon_DOr,
Information1 = data.Informations.Information1
});
}
}
}
.
이 방법은 작동하지만 효율적이지 않으며 내 UI를 차단합니다. '다른 경우'라는 메시지가없는 "PlayerData"객체를 만들 수있는 방법은 무엇입니까? 고맙습니다!
p.s :이 질문은이 필터 JSON 정보 필터
편집 :
여기에 내가 runtimebinderexcepetion을 얻은 방법은 다음과 같습니다.
List<PlayerData> datalist = new List<PlayerData>();
foreach (dynamic pl in timeline)
{
datalist.Add(new PlayerData
{
Name = pl.Player_Name ,
Country = pl.Country ,
BallonDor = pl.Personal_Honours.Ballon_Dor,
Information1 = pl.Other_Informations.Information1.name
});
}
. 해결책
일부 데이터에는 예를 들어 Personal_Honours
속성이 없으므로 예외가 있습니다.그런 다음 예외를 트리거하는 Ballon_Dor
참조 참조에서 null
속성에 액세스하려고했습니다.이 방법은 게시 된 샘플 JSON에서 작동합니다.
List<PlayerData> datalist = new List<PlayerData>();
foreach (dynamic pl in timeline)
{
//object initialization is done later just for the sake of easier debugging
//...so we can spot unexpected data which cause exception easily
var Name = pl.Player_Name;
var Country = pl.Country;
var BallonDor = pl.Personal_Honours != null ? pl.Personal_Honours.Ballon_Dor : null;
var Information1 = pl.Other_Informations.Information1.Count > 0 ?
pl.Other_Informations.Information1[0].name :
null;
datalist.Add(new PlayerData
{
Name = Name ,
Country = Country ,
BallonDor = BallonDor,
Information1 = Information1
});
}
.
... 그러나 위의 접근 방식은 우리가 가진 JSON 문자열의 일관성이 얼마나 일관되게되는지에 따라 여전히 오류가 발생하기 쉽습니다.더 많은 강력한 접근 방식은 @ l.b의 주석으로 제안 된대로 JSON 문자열을 맵핑 할 수있는 모델 클래스를 가질 수 있습니다.
제휴하지 않습니다 StackOverflow