请需要一些帮助。
我正在尝试将所有 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/"
},
}
]
}
您可以使用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 将所有字符串属性值转换为小写。
我希望这能帮助你解决你的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句