Я начинаю со следующего JSON ответа:
{
"d": {
"RowData": [
{
"GenreId": 11,
"GenreName": "Musical",
"subjecturl": "subjecturl_123",
"logourl": "logourl_1"
},
{
"GenreId": 12,
"GenreName": "kids",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_2"
},
{
"GenreId": 13,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_3"
},
{
"GenreId": 14,
"GenreName": "Musical",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_4"
},
{
"GenreId": 15,
"GenreName": "Music",
"subjecturl": "subjecturl_123",
"logourl": "logourl_5"
},
{
"GenreId": 16,
"GenreName": "Faimaly",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_6"
},
{
"GenreId": 17,
"GenreName": "other",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_7"
},
{
"GenreId": 18,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_8"
},
{
"GenreId": 19,
"GenreName": "kids",
"subjecturl": "subjecturl_123",
"logourl": "logourl_9"
},
{
"GenreId": 20,
"GenreName": "Musical",
"subjecturl": "subjecturl_123",
"logourl": "logourl_10"
},
{
"GenreId": 21,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_11"
}
]
}
}
Моя цель - преобразовать приведенный выше ответ в новый, как показано ниже:
{
"rows": [
{
"title": "Musical",
"items": [
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_1",
"subjecturlsplit": "1"
},
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_4",
"subjecturlsplit": "abc"
},
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_10",
"subjecturlsplit": "10"
}
]
},
{
"title": "kids",
"items": [
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_2",
"subjecturlsplit": "abc"
},
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_9",
"subjecturlsplit": "9"
}
]
},
{
"title": "Music",
"items": [
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_5",
"subjecturlsplit": "5"
}
]
},
{
"title": "other",
"items": [
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_3",
"subjecturlsplit": "3"
},
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_7",
"subjecturlsplit": "abc"
},
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_8",
"subjecturlsplit": "8"
},
{
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_11",
"subjecturlsplit": "11"
}
]
},
{
"title": "Faimaly",
"items": [
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_6",
"subjecturlsplit": "abc"
}
]
}
]
}
Я пробовал следующий запрос LINQ для выполнения указанного выше ответа:
var data = json["d"]?["RowData"]
.GroupBy(x => x["GenreName"])
.ToDictionary(g => g.Key, g => g.ToList())
.Select(kvp => new
{
title = kvp.Key,
items = kvp.Value.Select(x => new { hdsubjecturl = x["subjecturl"], url = x["logourl"], subjecturlsplit = "" })
});
var result = JObject.FromObject(new { rows = data });
Используя этот запрос, я сделал ответ, как показано ниже.
{
"rows": [
{
"title": "Musical",
"items": [
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_1",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_4",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_10",
"subjecturlsplit": ""
}
]
},
{
"title": "kids",
"items": [
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_2",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_9",
"subjecturlsplit": ""
}
]
},
{
"title": "Music",
"items": [
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_5",
"subjecturlsplit": ""
}
]
},
{
"title": "other",
"items": [
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_3",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_7",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_8",
"subjecturlsplit": ""
},
{
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_11",
"subjecturlsplit": ""
}
]
},
{
"title": "Faimaly",
"items": [
{
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_6",
"subjecturlsplit": ""
}
]
}
]
}
Обратите внимание, что subjecturlsplit
пусто для каждого элемента . Можно ли добавить условное logi c в этот запрос LINQ? Я хотел бы разделить значение hdsubjecturl
на подчеркивание (_
) и проверить, равна ли вторая часть abc
. Если это так, то я хочу установить subjecturlsplit
в поле hdsubjecturl
как есть. В противном случае я хочу разделить значение hdlogourl
на подчеркивание и установить subjecturlsplit
на вторую часть этого разделения. Или есть другой способ сделать это?