/*
//JSON FORMAT

{
"teamname": "My Team",
"etc": "master group",
"members": [{
"name": "Mad Dog",
"age": 36,
"job": "Engineer",
"sex": "male",
}, {
"name": "Angry Bird",
"age": 30,
"job": "self-employment",
"sex": "female",
}]
}

json parsing - JsonConvert.DeserializeObject
json 문자열을 클래스로 바로 파싱을 하려면 JsonConvert 객체의 DeserializeObject 함수를 이용하면 됩니다.

json 변환 - JsonConvert.SerializeObject
클래스를 json 문자열로 변환할때는 JsonConvert의 SerializeObject 를 이용하면 됩니다.

string json = JsonConvert.SerializeObject(Teams, Formatting.None);
*/



using System;
using System.Linq;
using System.Collections.Generic;


#if(true)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#endif

class Program
{
public static void Main(string[] args)
{
DeserializeObjectManage nm = new DeserializeObjectManage();
nm.running();
}
}
class DeserializeObjectManage
{
public class clTeams
{
public string teamname;         //json 항목명과 동일해야함 (대소문자 구분)
public string etc;              //json 항목명과 동일해야함 (대소문자 구분)

public List<clMember> members;  //json 항목명과 동일해야함 (대소문자 구분)
}

public class clMember
{
public string name;
public int age;
public string job;
public string sex;
}

public void running()
{
string jsontext = "{ \"teamname\": \"My Team\", \"etc\": \"master group\", \"members\": [{ \"name\": \"Mad Dog\", \"age\": 36, \"job\": \"Engineer\", \"sex\": \"male\", }, { \"name\": \"Angry Bird\", \"age\": 30, \"job\": \"self-employment\", \"sex\": \"female\", }]}";

clTeams Teams; //선언해준 클래스

Teams = JsonConvert.DeserializeObject<clTeams>(jsontext);

Console.WriteLine(Teams.members.Count);

for (int kk = 0; kk < Teams.members.Count; kk++)
{
Console.WriteLine(Teams.members[kk].name);
Console.WriteLine(Teams.members[kk].age);
Console.WriteLine(Teams.members[kk].job);
Console.WriteLine(Teams.members[kk].sex);
}
}
}

/*
csc /reference:Newtonsoft.Json.Net20.dll /out:form_6.exe form_6.cs
csc /reference:Newtonsoft.Json.Net20.dll /out:form_6.exe form_6.cs
csc /reference:Newtonsoft.Json.Net20.dll /out:form_6.exe form_6.cs
*/

+ Recent posts