{
"type": "example",
"token": "example",
"action_ts": "example",
"team": {
"id": "example",
"domain": "example"
},
"user": {
"id": "example",
"username": "example",
"team_id": "example",
"name": "example"
},
//
"blocks": [
{
"type": "example",
"block_id": "example/",
"text": {
"type": "example",
"text": "example",
"verbatim": false
}
},
// 원하는 정보는 여기 있습니다.
{
"type": "targetType",
"block_id": "example",
"text": {
"type": "example",
"text": "targetText",
"verbatim": false
}
},
public class DeserializeModel
{
...
public JsonArray Blocks { get; set;}
...
}
// 코드 품질 이슈....
public GetTextOfTarget(JsonArray DeserializeModel.Blocks)
{
foreach (var block in blocks)
{
var items = block.AsObject();
...
...
...
if (isTarget: true)
{
foreach (var item in items)
{
if (item.Key.Equals("text"))
{
foreach (var textItem in item.Value.AsObject())
{
if (textItem.Key.Equals("text"))
{
text = textItem.Value.ToString();
break;
}
}
break;
}
}
}
break;
}
}
중첩된 JosnObject에서 원하는 데이터를 찾기 위해 코드를 작성했습니다.
GetTextOfTarget가 if와 foreach문이 중첩의 중첩이 되서 가독성의 문제 등…이 있지 않나 생각이 듭니다.
역직렬화 Model을 작성하여 key에 매칭 시키는 방법도 있긴한데(DeserializeModel처럼), 여러 종류의 Json 데이터 값이 들어오고 그 것에 맞는 Model을 일일이 작성하는 것도 아닌거 같구요,
결론은 코드 품질을 높이고 싶은데, 좋은 방법을 모르겠습니다
분명히 누군가는 저와 같은 생각을 하고 같은 상황을 걲었을 텐데, 좋은 예제가 안 보이는군요.
보통 이런 경우 검색 키워드 선정이 잘 못 되었거나, 간단한건데 삽질하는 경우 더라구요.