Language/C#

[Formatter/JSON] Json To C# Online Converter

NewBigWater 2022. 3. 28. 17:38

응용 프로그램을 개발 하다 보면, 
Json이나, xml 포멧으로 데이터를 주고 받을 경우가 있다.
그럴경우, 데이터를 받을 수 있는 class를 생성하고, instance화하여, 관리하는
작업이 있을 경우가 있다.
이때, Online Convert를 활용하면
쉽게, Format을 class 구조로 변경하는 노가다? 작업이 없어진다.

# URL : https://json2csharp.com/json-to-csharp

 

Convert JSON to C# Online - Json2CSharp

 

json2csharp.com

1. Open API나 기타 등등으로 받아온 데이터
   {"response":{"header":{"resultCode":"00","resultMsg":"NORMAL_SERVICE"},"body":{"dataType":"JSON","items":{"item":[{"code":"A07_1","areaNo":"1100000000","date":"2022032806","today":"5","tomorrow":"6","dayaftertomorrow":"3","twodaysaftertomorrow":""}]},"pageNo":1,"numOfRows":10,"totalCount":1}}}

2. Convert

3. Project 적용
 1) NuGet 패키지 설치 : Newtonsoft.Json
  - 해당 패키지를 설치하면, json 문자열을 객체로 쉽게 변환할 수 있다. 

 2) 복사한 클래스 추가 및 Json string to Class

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Header
{
    public string resultCode { get; set; }
    public string resultMsg { get; set; }
}

public class Item
{
    public string code { get; set; }
    public string areaNo { get; set; }
    public string date { get; set; }
    public string today { get; set; }
    public string tomorrow { get; set; }
    public string dayaftertomorrow { get; set; }
    public string twodaysaftertomorrow { get; set; }
}

public class Items
{
    public List<Item> item { get; set; }
}

public class Body
{
    public string dataType { get; set; }
    public Items items { get; set; }
    public int pageNo { get; set; }
    public int numOfRows { get; set; }
    public int totalCount { get; set; }
}

public class Response
{
    public Header header { get; set; }
    public Body body { get; set; }
}

public class Root
{
    public Response response { get; set; }
}
-------------------------------------------------------------------------------------
// Json string to object
JsonConvert.SerializeObject(JsonConvert.DeserializeObject<Root>(results)

// json instance to json file
File.WriteAllText("result2.json", JsonConvert.SerializeObject(JsonConvert.DeserializeObject<Root>(results), Formatting.Indented));

4. Json file