티스토리 뷰

반응형

[unity/ wpf/c#] newton json Serialize/deSerialize(ToJson/ToObject)

 

#위 프로젝트는 Newton.json 라이브러리 설치가 필요합니다. 유니티의 경우 기본 설치되어 있기때문에 추가 설치가 필요없습니다.

 

#Serialize DeSerialize To Json

## 클래스로 구조를 만든다.

public class History
    {
        public string Title { get; set; }
        public string Language { get; set; }
        public List<HistoryTop> tops { get; set; }
        public List<HistoryBottom> bottoms { get; set; }
    }

    public class HistoryTop
    {
        public string Caption { get; set; }
        public string ImageUri { get; set; }
    }

    public class HistoryBottom
    {
        public string Year { get; set; }
        public string Content { get; set; }
    }

 

##샘플데이터를 만든다.

 
  List<History> historys;
 public void MakeData()
    {
        historys = new List<History>();
        historys.Add(MakeItemKor());
        historys.Add(MakeItemEng());
    }

    public History MakeItemKor()
    {
        History kor = new History();
        kor.Language = "kor";
        kor.Title = "한국어 타이틀";

        kor.tops = new List<HistoryTop>();
        kor.tops.Add(new HistoryTop { Caption = "캡션01", ImageUri = @"c:\a.jpg" });
        kor.tops.Add(new HistoryTop { Caption = "캡션02", ImageUri = @"c:\b.jpg" });
        kor.tops.Add(new HistoryTop { Caption = "캡션03", ImageUri = @"c:\c.jpg" });
        kor.tops.Add(new HistoryTop { Caption = "캡션04", ImageUri = @"c:\d.jpg" });
        kor.tops.Add(new HistoryTop { Caption = "캡션05", ImageUri = @"c:\e.jpg" });

        kor.bottoms = new List<HistoryBottom>();
        kor.bottoms.Add(new HistoryBottom { Content = "컨텐트01", Year = "1924" });
        kor.bottoms.Add(new HistoryBottom { Content = "컨텐트02", Year = "1934" });
        kor.bottoms.Add(new HistoryBottom { Content = "컨텐트03", Year = "1944" });
        kor.bottoms.Add(new HistoryBottom { Content = "컨텐트04", Year = "1954" });
        kor.bottoms.Add(new HistoryBottom { Content = "컨텐트05", Year = "1964" });
        return kor;
    }
    public History MakeItemEng()
    {
        History eng = new History();
        eng.Language = "eng";
        eng.Title = "english title";

        eng.tops = new List<HistoryTop>();
        eng.tops.Add(new HistoryTop { Caption = "Caption01", ImageUri = @"c:\a.jpg" });
        eng.tops.Add(new HistoryTop { Caption = "Caption02", ImageUri = @"c:\b.jpg" });
        eng.tops.Add(new HistoryTop { Caption = "Caption03", ImageUri = @"c:\c.jpg" });
        eng.tops.Add(new HistoryTop { Caption = "Caption04", ImageUri = @"c:\d.jpg" });
        eng.tops.Add(new HistoryTop { Caption = "Caption05", ImageUri = @"c:\e.jpg" });

        eng.bottoms = new List<HistoryBottom>();
        eng.bottoms.Add(new HistoryBottom { Content = "Content01", Year = "1924" });
        eng.bottoms.Add(new HistoryBottom { Content = "Content02", Year = "1934" });
        eng.bottoms.Add(new HistoryBottom { Content = "Content03", Year = "1944" });
        eng.bottoms.Add(new HistoryBottom { Content = "Content04", Year = "1954" });
        eng.bottoms.Add(new HistoryBottom { Content = "Content05", Year = "1964" });
        return eng;
    }

 

 

##객체화를 진행한다.

 public string ParsingToJson()
    {
        return JsonConvert.SerializeObject(historys);
    }

 

##결과값

[
  {
    "Title": "한국어 타이틀",
    "Language": "kor",
    "tops": [
      {
        "Caption": "캡션01",
        "ImageUri": "c:\\a.jpg"
      },
      {
        "Caption": "캡션02",
        "ImageUri": "c:\\b.jpg"
      },
      {
        "Caption": "캡션03",
        "ImageUri": "c:\\c.jpg"
      },
      {
        "Caption": "캡션04",
        "ImageUri": "c:\\d.jpg"
      },
      {
        "Caption": "캡션05",
        "ImageUri": "c:\\e.jpg"
      }
    ],
    "bottoms": [
      {
        "Year": "1924",
        "Content": "컨텐트01"
      },
      {
        "Year": "1934",
        "Content": "컨텐트02"
      },
      {
        "Year": "1944",
        "Content": "컨텐트03"
      },
      {
        "Year": "1954",
        "Content": "컨텐트04"
      },
      {
        "Year": "1964",
        "Content": "컨텐트05"
      }
    ]
  },
  {
    "Title": "english title",
    "Language": "eng",
    "tops": [
      {
        "Caption": "Caption01",
        "ImageUri": "c:\\a.jpg"
      },
      {
        "Caption": "Caption02",
        "ImageUri": "c:\\b.jpg"
      },
      {
        "Caption": "Caption03",
        "ImageUri": "c:\\c.jpg"
      },
      {
        "Caption": "Caption04",
        "ImageUri": "c:\\d.jpg"
      },
      {
        "Caption": "Caption05",
        "ImageUri": "c:\\e.jpg"
      }
    ],
    "bottoms": [
      {
        "Year": "1924",
        "Content": "Content01"
      },
      {
        "Year": "1934",
        "Content": "Content02"
      },
      {
        "Year": "1944",
        "Content": "Content03"
      },
      {
        "Year": "1954",
        "Content": "Content04"
      },
      {
        "Year": "1964",
        "Content": "Content05"
      }
    ]
  }
]

 

#Serialize 객체화 하는법

## 다음과 같이 너무 손쉽게 객체화가 가능하다.

  public List<History> ParsingToObject()
    {
        ReadText();
        return JsonConvert.DeserializeObject<List<History>>(parsingText);
    }

    public void ReadText()
    {
        string txtFile = $"{Application.streamingAssetsPath}/config.json";
        FileStream filestream = new FileStream(txtFile, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(filestream, System.Text.Encoding.UTF8);
        parsingText = sr.ReadToEnd();
        sr.Close();
        filestream.Close();
    }

 

#c# #json #unity #newton #wpf #toObject #toJson

반응형
댓글
반응형