.net web api를 사용하여 json을 가져 와서 각도에 대해 프런트 엔드로 반환합니다. json은 객체 또는 배열 일 수 있습니다. 내 코드는 현재 객체가 아닌 배열에서만 작동합니다. tryparse하거나 내용이 객체인지 배열인지 판단 할 방법을 찾아야합니다.Json 결과가 객체인지 배열인지 확인합니다.
여기에 내 코드
public HttpResponseMessage Get(string id)
{
string singleFilePath = String.Format("{0}/../Data/phones/{1}.json", AssemblyDirectory, id);
List<Phone> phones = new List<Phone>();
Phone phone = new Phone();
JsonSerializer serailizer = new JsonSerializer();
using (StreamReader json = File.OpenText(singleFilePath))
{
using (JsonTextReader reader = new JsonTextReader(json))
{
//if array do this
phones = serailizer.Deserialize<List<Phone>>(reader);
//if object do this
phone = serailizer.Deserialize<Phone>(reader);
}
}
HttpResponseMessage response = Request.CreateResponse<List<Phone>>(HttpStatusCode.OK, phones);
return response;
}
이 일을 상기하지 않을 수 있습니다 가장 좋은 방법입니다. 그저 지금 내가있는 곳.
첫 번째 charcter를 확인할 수 있습니다. isArray = content [0] == '[' –
@ johnny5 좋은 lib가 사용 가능할 때 수동으로 파싱하는 것은 일반적으로 좋은 생각이 아닙니다. 예를 들어, 공백을 확인하는 것을 잊었습니다. :) "[]"'은 유효한 json 배열입니다. – dcastro
그 이유는 해킹이기 때문에 답변으로 게시하지 않았지만 가치있는 메모입니다. –