C#의 Json rest api 응답을 해석하는 중
C#을 사용하여 rest api json response에서 값을 가져오려고 합니다.
다음 코드가 있습니다.
client.BaseUrl = "https://api.cloud.appcelerator.com";
request.Resource = "/v1/chats/create.json?key=" + cac.AppCode.ToString();
request.Method = Method.POST;
request.AddUrlSegment("appkey", "key");
var response = client.Execute(request);
"응답" 메시지에서 다음과 같은 json 내용을 받았습니다.
{
"meta": {
"code": 200,
"status": "ok",
"method_name": "createChatMessage"
},
"response": {
"chats": [
{
"id": "521cfcd840926a0b3500449e",
"created_at": "2013-08-27T19:24:08+0000",
"updated_at": "2013-08-27T19:24:08+0000",
"message": " join to the chat group, welcome …",
"from": {
"id": "520f41e125e74b0b2400130a",
"first_name": "Administrator",
"created_at": "2013-08-17T09:26:57+0000",
"updated_at": "2013-08-27T19:23:10+0000",
"external_accounts": [
],
"email": "roy@tomax.co.il",
"confirmed_at": "2013-08-17T09:26:57+0000",
"username": "admin",
"admin": "true",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
}
},
"chat_group": {
"id": "521cfcd840926a0b3500449d",
"created_at": "2013-08-27T19:24:08+0000",
"updated_at": "2013-08-27T19:24:08+0000",
"message": " join to the chat group, welcome …",
"participate_users": [
{
"id": "520f41e125e74b0b2400130a",
"first_name": "Administrator",
"created_at": "2013-08-17T09:26:57+0000",
"updated_at": "2013-08-27T19:23:10+0000",
"external_accounts": [
],
"email": "roy@tomax.co.il",
"confirmed_at": "2013-08-17T09:26:57+0000",
"username": "admin",
"admin": "true",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
}
}
]
}
}
]
}
}
반환된 json 응답 결과 메시지에서 "id"의 중첩된 값 "521cfcd840926a0b3500449e"를 가져오려면 어떻게 해야 합니까?
C#을 사용하고 있습니다.
1> 이 남스페이스를 추가합니다.뉴튼소프트를 사용해서요.Json.Linq;
2 > 이 소스 코드를 사용합니다.
JObject joResponse = JObject.Parse(response);
JObject ojObject = (JObject)joResponse["response"];
JArray array= (JArray)ojObject ["chats"];
int id = Convert.ToInt32(array[0].toString());
- 데이터와 일치하는 클래스를 만듭니다.
- JSON을 사용합니다.NET: JSON 데이터를 일반 C# 객체로 변환합니다.
스텝 1: 뛰어난 툴인 http://json2csharp.com/에서 생성된 결과는 다음과 같습니다.
순서 2:JToken.Parse(...).ToObject<RootObject>().
public class Meta
{
public int code { get; set; }
public string status { get; set; }
public string method_name { get; set; }
}
public class Photos
{
public int total_count { get; set; }
}
public class Storage
{
public int used { get; set; }
}
public class Stats
{
public Photos photos { get; set; }
public Storage storage { get; set; }
}
public class From
{
public string id { get; set; }
public string first_name { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public List<object> external_accounts { get; set; }
public string email { get; set; }
public string confirmed_at { get; set; }
public string username { get; set; }
public string admin { get; set; }
public Stats stats { get; set; }
}
public class ParticipateUser
{
public string id { get; set; }
public string first_name { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public List<object> external_accounts { get; set; }
public string email { get; set; }
public string confirmed_at { get; set; }
public string username { get; set; }
public string admin { get; set; }
public Stats stats { get; set; }
}
public class ChatGroup
{
public string id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string message { get; set; }
public List<ParticipateUser> participate_users { get; set; }
}
public class Chat
{
public string id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string message { get; set; }
public From from { get; set; }
public ChatGroup chat_group { get; set; }
}
public class Response
{
public List<Chat> chats { get; set; }
}
public class RootObject
{
public Meta meta { get; set; }
public Response response { get; set; }
}
Json에 매핑되는 C# 클래스를 만들고Newsoft JsonConvert디시리얼라이즈 합니다.
예를 들어 다음과 같습니다.
public Class MyResponse
{
public Meta Meta { get; set; }
public Response Response { get; set; }
}
언급URL : https://stackoverflow.com/questions/18490599/parsing-json-rest-api-response-in-c-sharp
'source' 카테고리의 다른 글
| 각도를 정의할 때 배열 표기법을 사용하는 이유JS 컨트롤러 (0) | 2023.03.06 |
|---|---|
| Oracle SqlPlus - 출력을 파일에 저장하지만 화면에 표시되지 않음 (0) | 2023.03.06 |
| React에서 조건부 스타일을 처리하는 올바른 방법 (0) | 2023.03.06 |
| jQuery ready 기능이 WordPress에서 작동하지 않습니다. (0) | 2023.03.06 |
| 반응 원어민에서 터치 가능 영역 제어 (0) | 2023.03.06 |