Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Parse this Json string in c#

Tags:

json

c#

I tried using Newtonsoft.Json to deserialize this json string, but I'm not getting the desired output. my json string is

[
{
"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"
}
]

I just want the value of key when value of id=1. Generally json starts with {}(curly bracket) but here it is like [](square bracket). I've seen many examples but none worked for me.

like image 729
Sumit Chourasia Avatar asked Jan 22 '26 03:01

Sumit Chourasia


1 Answers

Generally json starts with {} (curly bracket), but here it is like [] (square bracket).

This is because you got an array of objects, not a single object. Arrays are serialized with square brackets around them. You should deserialize it into an array, and then grab the object at the index of interest.

like image 111
Sergey Kalinichenko Avatar answered Jan 23 '26 16:01

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!