請需要一些幫助。
我正在嘗試將所有 json 值(不是鍵)轉換為小寫。我瀏覽并發現僅如何轉換屬性名稱(鍵),而不是屬性值。
我花了一整天的時間試圖弄清楚該怎么做。
有類似的東西
"name": "luke skywalker",
"hair_color": "blond",
"skin_color": "fair",
以下是我目前正在嘗試的代碼。它不起作用。
private static void ChangePropertiesToLowerCase(JObject jsonObject)
{
foreach (var property in jsonObject.Properties().ToList())
{
if (property.Value.Type == JTokenType.Object)// replace property names in child object
ChangePropertiesToLowerCase((JObject)property.Value);
property.Replace(new JProperty(property.Name.ToLower(), property.Value));// properties are read-only, so we have to replace them
}
}
下面是json檔案
{
"count": 82,
"next": "https://swapi.dev/api/people/?page=2",
"previous": null,
"results": [
{
"name": "Luke Skywalker",
"height": "172",
"mass": "77",
"hair_color": "blond",
"skin_color": "fair",
"eye_color": "blue",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "https://swapi.dev/api/planets/1/",
"films": [
"https://swapi.dev/api/films/1/",
"https://swapi.dev/api/films/2/",
"https://swapi.dev/api/films/3/",
"https://swapi.dev/api/films/6/"
],
"species": [],
"vehicles": [
"https://swapi.dev/api/vehicles/14/",
"https://swapi.dev/api/vehicles/30/"
],
"starships": [
"https://swapi.dev/api/starships/12/",
"https://swapi.dev/api/starships/22/"
],
"created": "2014-12-09T13:50:51.644000Z",
"edited": "2014-12-20T21:17:56.891000Z",
"url": "https://swapi.dev/api/people/1/"
},
{
"name": "C-3PO",
"height": "167",
"mass": "75",
"hair_color": "n/a",
"skin_color": "gold",
"eye_color": "yellow",
"birth_year": "112BBY",
"gender": "n/a",
"homeworld": "https://swapi.dev/api/planets/1/",
"films": [
"https://swapi.dev/api/films/1/",
"https://swapi.dev/api/films/2/",
"https://swapi.dev/api/films/3/",
"https://swapi.dev/api/films/4/",
"https://swapi.dev/api/films/5/",
"https://swapi.dev/api/films/6/"
],
"species": [
"https://swapi.dev/api/species/2/"
],
"vehicles": [],
"starships": [],
"created": "2014-12-10T15:10:51.357000Z",
"edited": "2014-12-20T21:17:50.309000Z",
"url": "https://swapi.dev/api/people/2/"
},
}
]
}
uj5u.com熱心網友回復:
您可以使用JsonConverter來解決您的問題。
您可以創建自己的 JsonConverter 來處理字串值。在下面的代碼中,我創建了這個客戶 JsonConverter,以便在寫入 JSON 時將字串值轉換為小寫。
public class StringConverter : JsonConverter<string>
{
public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer)
{
// converting to lower case string while writing to JSON outcome.
writer.WriteValue(value.ToLower());
}
public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer)
{
// returning the value without change while deserializing.
return (string)reader.Value;
}
}
以下是您可以使用上述轉換器的方式。
// let say your class name is SearchResult;
// JSON string is stored in jsonString variable.
var searchResult = JsonConvert.Deserialize<SearchResult>(jsonString);
var jsonStringLowerCase = JsonConvert.Serialize(jsonString, new StringConverter());
使用上面的代碼jsonStringLowerCase,JSON 將所有字串屬性值轉換為小寫。
我希望這能幫助你解決你的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/492107.html
標籤:C# 。网 asp.net-mvc asp.net 核心 json.net
