我正在进行一个API调用,该调用返回JSON(到名为jsonString
的变量中)。在JSON数据上进行Console.WriteLine(jsonString)
可以显示以下内容:
{“ contentType”:null,“ serializerSettings”:null,“ statusCode”:null,“ value”:“ {\” contentType \“:null,\” serializerSettings \“:null,\” statusCode \“: null,\“ value \”:{\“ 10 \”:\“ ALABAMA \”,\“ 11 \”:\“ ALASKA \”,\“ 12 \”:\“ ARIZONA \”,\“ 13 \” :\“ ARKANSAS \”,\“ 14 \”:\“ CALIFORNIA \”,\“ 15 \”:\“ COLORADO \”,\“ 16 \”:\“ CONNECTICUT \”,\“ 17 \”:\ “ DELAWARE \”,\“ 18 \”:\“ FLORIDA \”,\“ 19 \”:\“ GEORGIA \”,\“ 20 \”:\“ HAWAII \”,\“ 21 \”:\“ IDAHO \“,\” 22 \“:\” ILLINOIS \“,\” 23 \“:\” INDIANA \“,\” 24 \“:\” IOWA \“,\” 25 \“:\” KANSAS \“ ,\“ 26 \”:\“肯塔基州”,\“ 27 \”:\“ LOUISIANA \”,\“ 28 \”:\“ MAINE \”,\“ 29 \”:\“马里兰州”,\ “ 30 \”:\“ MASSACHUSETTS \”,\“ 31 \”:\“ MICHIGAN \”,\“ 32 \”:\“ MINNESOTA \”,\“ 33 \”:\“ MISSISSIPPI \”,\“ 34 \“:\” MISSOURI \“,\” 35 \“:\” MONTANA \“,\” 36 \“:\” NEBRASKA \“,\” 37 \“:\” NEVADA \“,\” 38 \“ :\“ NEW HAMPSHIRE \”,\“ 39 \”:\“ NEW JERSEY \”,\“ 40 \”:\“ NEW MEXICO \”,\“ 41 \”:\“ NEW YORK \”,\“ 42 \“:\” NORTH CAROLINA \“,\” 43 \“:\” NORTH DAKOTA \“,\” 44 \“:\” OHIO \“,\” 45 \“:\” OKLAHOMA \“,\” 46 \“:\” OREGON \“,\” 47 \“:\” PENNSYLVANIA \“,\” 48 \“:\” RHODE ISLAND \“,\” 49 \“:\” SOUTH CAROLINA \“,\” 50 \“:\” SOUTH DAKOTA \“,\ “ 51 \”:\“ TENNESSEE \”,\“ 52 \”:\“ TEXAS \”,\“ 53 \”:\“ UTAH \”,\“ 54 \”:\“ VERMONT \”,\“ 55 \“:\” VIRGINIA \“,\” 56 \“:\”华盛顿\“,\” 57 \“:\” WEST VIRGINIA \“,\” 58 \“:\” WISCONSIN \“,\” 59 \ “:\” WYOMING \“}}”}
我正尝试将其反序列化为如下所示的类:
public class LookupValuesResponse
{
[JsonProperty("value")]
public Dictionary<int, string> LookupValues;
[JsonProperty("statusCode")]
public string Status;
}
我已经尝试过了:
LookupValuesResponse lookupresponse = JsonConvert.DeserializeObject<LookupValuesResponse>(jsonString);
但是,我收到一个无法反序列化JSON的错误:
System.ArgumentException:无法将System.String强制转换或转换为System.Collections.Generic.Dictionary`2 [System.Int32,System.String]。
答案 0 :(得分:0)
我需要更改被调用的API返回其结果的方式。以前我有:
return Ok(Json(response.Content.ReadAsStringAsync().Result));
相反,我需要:
return Ok(JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result));