https://docs.microsoft.com/ko-kr/nuget/tools/package-manager-ui
c#에서 Json 파싱하기
NuGet 패키지 를 통해서 설치하기
비주얼 스튜디오에서 Nuget 패키지 관리자 실행
Json.NET
Serialize JSON
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
string json = JsonConvert.SerializeObject(product);
// {
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
// }
Deserialize JSON
string json = @"{
'Name': 'Bad Boys',
'ReleaseDate': '1995-4-7T00:00:00',
'Genres': [
'Action',
'Comedy'
]
}";
Movie m = JsonConvert.DeserializeObject<Movie>(json);
string name = m.Name;
// Bad Boys
LINQ to JSON
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
JObject o = new JObject();
o["MyArray"] = array;
string json = o.ToString();
// {
// "MyArray": [
// "Manual text",
// "2000-05-23T00:00:00"
// ]
// }
파싱예제
아래와 같은 JSON 형태
"class": "go.GraphLinksModel",
"linkKeyProperty": "stepId",
"modelData": {"position":"-5 -5"},
"nodeDataArray": [
{"key":11, "category":"start", "text":"Start", "loc":"288 109"},
{"key":101, "category":"message", "text":"Message", "items":[ {"name":"Name", "value":"Message"},{"name":"Type", "value":"Step"},{"name":"Category", "value":"대분류"},{"name":"Size", "value":"120"},{"name":"Max Length", "value":"20"} ], "loc":"292 181"}
],
"linkDataArray": [ {"from":11, "to":101, "stepId":-1, "items":[ {"name":"link", "value":"value1"},{"name":"propery1", "value":"value1"} ], "points":[288,129.5,288,139.5,288,147.1257080078125,292,147.1257080078125,292,154.751416015625,292,164.751416015625]}
private void Form1_Load(object sender, EventArgs e)
{
string s = this.textBox1.Text;
//var m = JsonConvert.DeserializeObject<Dictionary<string, object>>(s);
//if (m.TryGetValue("nodeDataArray", out result))
//{
// var ss = result;
//}
//object result = "";
JObject jo = JObject.Parse(s);
var a = jo.SelectToken("nodeDataArray");
var cnt = a.Count();
foreach (var item in a)
{
var key = item.SelectToken("key").ToString();
var category = item.SelectToken("category").ToString();
var text = item.SelectToken("text").ToString();
var items = item.SelectToken("items");
if(items != null)
{
foreach (var token in items)
{
var name = String.Format("{0}", token.SelectToken("name"));
var value = String.Format("{0}", token.SelectToken("value"));
}
}
}
}
'C#.NET' 카테고리의 다른 글
Visual Studio 용 Visual C++ 재배포 가능 패키지 다운로드 (0) | 2023.04.11 |
---|---|
C# (Attribute) Obsolete 사용안하는 메소드 표시 (0) | 2019.08.21 |
RTF 포멧을 HTML 포멧으로 변경하기 (0) | 2016.08.23 |
RicherTextBox 를 이용한 이미지 저장 및 포멧 저장 (0) | 2016.08.20 |
IIS 32비트 허용 (0) | 2013.08.22 |
Enum 데이터바인딩 및 가져오기 (0) | 2013.05.14 |
GroupBox Control Extensions (0) | 2013.05.06 |
[ Assembly ,어셈블리 ] 클래스 이름으로 동적으로 컨트롤 생성하기 (1) | 2013.03.08 |
개체의 선입선출(FIFO) / LIFO(후입선출) (0) | 2012.06.27 |
[ GMap.NET ] c# 에서 구글맵,빙맵등을 을 빠르고 간편하게 개발 (1) | 2012.03.13 |