再會!
我在反序列化 JSON 字串時遇到問題
這是我的功能:
public static void Deserialised()
{
string jsonString = "{\"data\":{\"merchantRefNo\":\"foo\","
"\"responseText\":\"Approved\","
"\"status\":\"A\","
"\"txnDate\":\"20220321\","
"\"txnId\":\"000067\","
"\"txnTime\":\"1049\","
"\"txnType\":\"sale\","
"\"amt\":\"109\"},"
"\"dataType\":\"trans\"}";
Root myDeserializedJson = JsonConvert.DeserializeObject<Root>(jsonString); //Error popup here!
foreach (var datas in myDeserializedJson.data)
{
Console.WriteLine(datas.merchantRefNo);
}
}
這是我的模型:
public class Data
{
public string merchantRefNo { get; set; }
public string responseText { get; set; }
public string status { get; set; }
public string txnDate { get; set; }
public string txnId { get; set; }
public string txnTime { get; set; }
public string txnType { get; set; }
public string amt { get; set; }
}
public class Root
{
public List<Data> data { get; set; }
public string dataType { get; set; }
}
錯誤:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[fortestings.Program Data]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'data.merchantRefNo', line 1, position 25.'
uj5u.com熱心網友回復:
在您的Root班級中,更改List<Data> data為Data data,然后您可以根據班級JSON的需要反序列化您的字串Root:
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string jsonString = "{\"data\":{\"merchantRefNo\":\"foo\","
"\"responseText\":\"Approved\","
"\"status\":\"A\","
"\"txnDate\":\"20220321\","
"\"txnId\":\"000067\","
"\"txnTime\":\"1049\","
"\"txnType\":\"sale\","
"\"amt\":\"109\"},"
"\"dataType\":\"trans\"}";
var result =JsonConvert.DeserializeObject<Root>(jsonString);
Console.WriteLine(result.dataType);
Console.WriteLine(result.data.merchantRefNo);
Console.WriteLine(result.data.status);
Console.WriteLine(result.data.txnId);
}
}
public class Data
{
public string merchantRefNo { get; set; }
public string responseText { get; set; }
public string status { get; set; }
public string txnDate { get; set; }
public string txnId { get; set; }
public string txnTime { get; set; }
public string txnType { get; set; }
public string amt { get; set; }
}
public class Root
{
public Data data { get; set; }
public string dataType { get; set; }
}
輸出:
trans
foo
A
000067
您可以在這里找到一個作業示例:https ://dotnetfiddle.net/8Nsl9m
uj5u.com熱心網友回復:
{“資料”:{“merchantRefNo”:“foo”,“responseText”:“已批準”,“狀態”:“A”,“txnDate”:“20220321”,“txnId”:“000067”,“txnTime”: "1049", "txnType":"sale", "amt":"109" }, "dataType":"trans" }
這是您的根物件 JSON,但您的班級需要一個資料串列
public class Root
{
public **List<Data>** data { get; set; }
public string dataType { get; set; }
}
因此,對于反序列化 json 與您的模型不匹配。您的 Json 應如下所示 { "data":[{ "merchantRefNo":"foo", "responseText":"Approved", "status":"A", "txnDate":"20220321", "txnId":" 000067", "txnTime":"1049", "txnType":"sale", "amt":"109" }], "dataType":"trans" }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445831.html
上一篇:如何從postgresql中的json陣列列中獲取json值
下一篇:_InternalLinkedHashMap<String,dynamic>'不是'FutureOr<List<dynamic>>型別的子型別
