복잡한 JSON 객체가 있습니다. 문제가 있습니다. 문제는 때때로 JSON 모델 내의 하위 항목 목록이 각 요소에 존재하지 않을 수 있다는 것입니다. 예. 완전히 실종되었다.루프 JSON 객체가 항상 존재하지 않을 수 있습니다.
json으로 개체 모두를 반환하는 몇 가지 요소에 대해, 나는 종종 2를 얻을 수 있습니다 또는 3 완전 내가 적어도 일부 내 개체 모델을 알 수 있도록 항목을 반환, 변화, 작동하지만 예를 들어 이름 1 아래에서 모델이 성공적으로 반환됩니다. 그런 다음 구문 분석 오류 이름 2. 따라서 예외없이 전체 목록을 반복 할 수는 없습니다.
"Object reference not set to an instance of an object"
Try Catch 블록 내에서 출력을 래핑하려고했으나 오류가 여전히 반환됩니다.
샘플
[
{
"name": "Name1",
"description": "",
"location": "ANY",
"inputs": [
{
"name": "input1",
"required": true,
"description": "some short description"
},
{
"name": "input2",
"required": true,
"description": "another short description"
}
],
"outputs": [
{
"name": "output1",
"required": false
},
{
"name": "outpu2",
"required": false
}
]
},
{
"name": "Name2",
"description": "some long description",
"location": "ANY",
"inputs": [ {
"name": "input1",
"required": false,
"description" : "some short description of the input"
}]
},
{
"name": "Name3",
"description": "",
"location": "ANY"
}
]
내 C# 정의는이 JSON 객체에 대한 첫 번째 참조를 위해 일하는 모든 사용 사례와 JSON, 그러나 "NAME2 및"에 대한 NAME3 객체 참조 인스턴스로 설정되지 않았습니다 "나는 이것이 무엇입니까" 개체의 "오류.
내 C# JSON 정의
public class inputParameters
{
[JsonProperty("name")]
public string inputName { get; set; }
[JsonProperty("required")]
public bool inputRequired {get; set;}
[JsonProperty("description")]
public string inputDescription {get; set;}
}
public class outputParameters
{
public string outputName { get; set; }
public bool outputRequired { get; set; }
}
public class JsonObject
{
[JsonProperty("name")]
public string ProcessName { get; set; }
[JsonProperty("description")]
public string ProcessDescription { get; set; }
[JsonProperty("peerLocation")]
public string PeerLocation { get; set; }
public List<inputParameters> InputParameters { get; set; }
public List<outputParameters> OutputParameters{ get; set; }
}
그리고 내 역 직렬화 객체와는
,369 루프var Object = JsonConvert.DeserializeObject <List<JsonObject>>(txt);
foreach (JsonObject JsonObject in Object)
{
Console.WriteLine("Process Name: " + JsonObject.ProcessName);
Console.WriteLine("PeerLoacatoin: " + JsonObject.PeerLocation);
Console.WriteLine("Process Description: " +JsonObject.ProcessDescription);
foreach (var InputParams in JsonObject.InputParameters)
{
try
{
Console.WriteLine("Input Name: " + InputParams.inputName);
Console.WriteLine("Input Required: " + InputParams.inputRequired);
Console.WriteLine("Input Description: " + InputParams.inputDescription);
}
catch(Exception)
{
InputParams.inputName = "";
InputParams.inputRequired = false;
InputParams.inputDescription = "";
}
}
foreach (var OutputParams in JsonObject.OutputParameters)
{
try
{
Console.WriteLine("Output Name: " + OutputParams.outputName);
Console.WriteLine("Output Required: " + OutputParams.outputRequired);
}
catch (Exception)
{
OutputParams.outputName = "";
OutputParams.outputRequired = false;
}
}
Console.WriteLine();
왜 이것을 JsonObject 목록에 역 직렬화하겠습니까? 그것을 적절한 .NET 유형으로 deserialize하고 그것을 조작하는 것이 더 쉽지 않을까요? – EJoshuaS
그 이점은 무엇입니까? 구현하려면 어떻게해야합니까? – Autonomic