我尝试使用 Newtonsoft.Json 反序列化这个 json 字符串,但我没有得到所需的输出。我的json字符串是
[
{
"id": 1,
"key": "Residential Homeowner",
"i18nText": "unknown message code DB_ENUM_UserType_residentialhomeowner",
"i18nKey": "DB_ENUM_UserType_residentialhomeowner"
},
{
"id": 8,
"key": "VAR Dealer \/ Builder",
"i18nText": "unknown message code DB_ENUM_UserType_vardealer\/builder",
"i18nKey": "DB_ENUM_UserType_vardealer\/builder"
},
{
"id": 2,
"key": "Administrator",
"i18nText": "unknown message code DB_ENUM_UserType_administrator",
"i18nKey": "DB_ENUM_UserType_administrator"
},
{
"id": 9998,
"key": "TempGuidUser",
"i18nText": "unknown message code DB_ENUM_UserType_tempguiduser",
"i18nKey": "DB_ENUM_UserType_tempguiduser"
},
{
"id": 9999,
"key": "GuidUser",
"i18nText": "unknown message code DB_ENUM_UserType_guiduser",
"i18nKey": "DB_ENUM_UserType_guiduser"
}
]
Run Code Online (Sandbox Code Playgroud)
当 id=1 的值时,我只想要键的值。通常json以{}(大括号)开头,但这里就像[](方括号)。我看过很多例子,但没有一个对我有用。
这是一篇解决 C# 中 JSON 解析的相关帖子:C# JSON Parsing。
如果括号有问题,只需使用:
string json = inputJson.Trim().Trim('[',']');
Run Code Online (Sandbox Code Playgroud)
如果 id 的最小值可以为 1,那么这应该有效:
string GetKey(string inputJson)
{
string key = inputJson.Substring(inputJson.IndexOf("key")+5);
key = key.Substring(key.IndexOf("\""), key.IndexOf(",")-key.IndexOf("\""));
key = key.Trim('\"');
return key;
}
Run Code Online (Sandbox Code Playgroud)
一般json以
{}(大括号)开头,但这里是[](方括号)。
这是因为您获得了一组对象,而不是单个对象。数组使用方括号围绕它们进行序列化。您应该将其反序列化为一个数组,然后在感兴趣的索引处抓取对象。
| 归档时间: |
|
| 查看次数: |
7228 次 |
| 最近记录: |