Json이 있습니다.
[
[
[
{
"origin": [
-15.2941064136735,
-0.43948581648487,
4.83674058264479
],
"dimensions": [
10.4597624323399,
11.6903227184975,
9.67348116528958
],
"primitive": "block"
}
],
[
{
"origin": [
-15.2941064136735,
-0.43948581648487,
4.83674058264479
],
"dimensions": [
10.4597624323399,
11.6903227184975,
9.67348116528958
],
"primitive": "block"
}
],
[
{
"origin": [
-15.2941064136735,
-0.43948581648487,
4.83674058264479
],
"dimensions": [
10.4597624323399,
11.6903227184975,
9.67348116528958
],
"primitive": "block"
}
]
]
]
그리고이 Json의 모델 :
public class BoxConverter
{
[JsonProperty("origin")]
public List<double> Origin { get; set; }
[JsonProperty("dimensions")]
public List<double> Dimensions { get; set; }
[JsonProperty("primitive")]
public string Primitive { get; set; }
}
그리고 파일에있는 json에서 객체 목록을 얻으려고합니다.
string strLocal = File.ReadAllText("2.txt");
var convertLocal = JsonConvert.DeserializeObject<List<BoxConverter>>(strLocal);
하지만 예외가 있습니다.
형식이 올바르게 역 직렬화하려면 JSON 개체 (예 : { "name": "value"})가 필요하므로 현재 JSON 배열 (예 : [1,2,3])을 형식으로 역 직렬화 할 수 없습니다.
이 오류를 수정하려면 JSON을 JSON 개체 (예 : { "name": "value"})로 변경하거나 deserialized 형식을 배열 또는 List와 같은 컬렉션 인터페이스 (예 : ICollection, IList)를 구현하는 형식으로 변경합니다. JSON 배열에서 역 직렬화됩니다. JsonArrayAttribute를 유형에 추가하여 JSON 배열에서 강제로 역 직렬화 할 수도 있습니다.
경로 '[0]', 줄 1, 위치 2
내가 뭘 잘못하고 있니?
최신 정보
이 JSON은 완벽하게 작동합니다.
[{"attributes":{"materialProperties":{"color":"red","wireframe":false}},"dimensions":[10.4597624323399,11.6903227184975,9.67348116528958],"origin":[-15.2941064136735,-0.43948581648487,4.83674058264479],"primitive":"block"}]
귀하 JSON
는 list of a list of a list of BoxConverter
.
다음을 수행해야합니다.
var convertLocal = JsonConvert.DeserializeObject<List<List<List<BoxConverter>>>>(strLocal)
귀하의 BoxConverter
클래스는 괜찮습니다.
json
알 수없는 횟수만큼 중첩 된 경우 (아마도 이것이 처음에 발생한 이유를 설명해야 함) 다음과 같이 할 수 있습니다.
var superHack = JsonConvert.DeserializeObject<dynamic>(json);
var currentObject = superHack;
while(currentObject is JArray)
currentObject = currentObject[0];
var foundObject = currentObject as JObject;
var convertLocal = foundObject.ToObject<BoxConverter>();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다