C \ # 해석 JSON

3504 단어 json

C \ # JSON 을 분석 하 는 것 은 골 치 아 픈 일이 다. 인터넷 의 각종 자료 가 비교적 낡 아서 어느 날 나 는 비교적 좋 은 방법 을 찾 았 다. 다음 과 같이 공유 했다.
걸 어 오 면 NuGet 을 열 고 Newtonsoft. JSon 을 검색 한 다음 설치 해 야 합 니 다.이것 은 매우 우수한 제 이 슨 분석 창고 입 니 다. 매우 우수 하고 우수한 날 아 오 릅 니 다!
그리고 데이터 계약 (DataContract) 을 정의 하 세 요. 네 ~ 한 가지 만 정의 하면 됩 니 다.
마지막 으로 아래 코드 로 해석 하 는 것 은 매우 간단 하 다.
 1         public static T JsonDecode<T>(string PathOrData)

 2         {
3        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
4 JsonSerializer json = new JsonSerializer(); 5 json.NullValueHandling = NullValueHandling.Ignore; 6 json.ObjectCreationHandling = ObjectCreationHandling.Replace; 7 json.MissingMemberHandling = MissingMemberHandling.Ignore; 8 json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 9 TextReader Stream = null; 10 try 11 { 12 IsolatedStorageFileStream Location = new IsolatedStorageFileStream(PathOrData, FileMode.Open, storage); 13 Stream = (TextReader)new StreamReader(Location); 14 } 15 catch (Exception e) 16 { 17 Stream = (TextReader)new StringReader(PathOrData); 18 } 19 JsonTextReader Reader = new JsonTextReader(Stream); 20 T Result = (T)json.Deserialize(Reader, typeof(T)); 21 Reader.Close(); 22 return Result; 23 }

좋은 웹페이지 즐겨찾기