對不起,我的英語不好。但我需要這個方法的一些幫助(ToObject)
我有這門課
namespace Proyects
{
public class AProductType
{
public DateTime CreationDate { get; set; }
public string Product { get; set; }
}
public class AProduct
{
public AProductType A_ProductType { get; set; }
}
public class AProductPlantType
{
public string SerialNumberProfile { get; set; }
}
public class ToPlant
{
public List<AProductPlantType> A_ProductPlantType { get; set; }
}
public class AProductDescriptionType
{
public string Language { get; set; }
public string Product { get; set; }
public string ProductDescription { get; set; }
}
public class ToDescription
{
public List<AProductDescriptionType> A_ProductDescriptionType { get; set; }
}
public class Root
{
public AProduct A_Product { get; set; }
public ToPlant to_Plant { get; set; }
public ToDescription to_Description { get; set; }
}
}
我目前正在使用 Root 將 Jtoken 保存在那里。但我無法保存來自 Root 類的屬性的資料。
例如:
var object= myJson.ToObject<Root>();
如果我嘗試從AProductType保存關于propiety Product 的資料,我無法使用 ToOject 訪問那里。我嘗試這樣的事情
var object= myJson.ToObject<Root>().A_Product.A_ProductType.Product;
并且不作業,物件變數變為空。我需要一些方法來保存這個復雜物件周圍的資料,然后保存在Root的屬性中。
真的對不起我的英語,謝謝!!!
編輯:Json 檔案
{
"A_Product": {
"A_ProductType": {
"CreationDate": "2020-01-17T00:00:00",
"Product": "158"
}
},
"to_Plant": {
"A_ProductPlantType": [
{
"SerialNumberProfile": "E001"
}
]
},
"to_Description": {
"A_ProductDescriptionType": [
{
"Language": "EN",
"Product": "158",
"ProductDescription": "Terminal LaP (nro de serie equipo)"
},
{
"Language": "ES",
"Product": "158",
"ProductDescription": "Terminal LaP"
}
]
}
}
編輯2:
private static List<string> retrieveData(JObject ob, List<Root> listaObjetos)
{
List<string> ListaCodigoProducto = new List<string>();
Root objetoRot = new Root();
var A_Product = ob["A_Product"];
if (A_Product.HasValues)
{
var validacion = ob["A_Product"]["A_ProductType"];
if (validacion.Type == JTokenType.Object)
{
var objeto = validacion.ToObject<AProductType>();
ListaCodigoProducto.Add(objeto.Product);
objetoRot.A_Product.A_ProductType.Product = objeto.Product;
listaObjetos.Add(objetoRot);
}
當我嘗試將產品編號保存在
objetoRot.A_Product.A_ProductType.Product
它顯示 NullReference 例外,我無法訪問 Root 物件中的屬性
uj5u.com熱心網友回復:
反序列化代碼作業得很好。您的問題是您正在訪問不存在的物件。
當你說Root objetoRot = new Root();,你正在創造一個新的,空的Root。其A_Product值為null。幾行下來,你說objetoRot.A_Product.A_ProductType.Product = objeto.Product;。但是你不能得到objetoRot.A_Product.A_ProductType,因為沒有objetoRot.A_Product訪問屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416645.html
標籤:
下一篇:如何獲取請求-WOOF
