我正在尝试在json中添加字段,同时反序列化来自服务器的响应,然后将其存储到数据库中
这是Response的模型
public class Response : RealmObject
{
[JsonConverter(typeof(QuotationConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IList<Quotation> quotationsList { get; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IList<Order> ordersList { get; }
}
Run Code Online (Sandbox Code Playgroud)
QuotationConverter代码,我从另一个json获取客户名称并将其存储到报价中
protected override IList<Quotation> parseArray(Type objectType, JArray jsonArray)
{
try
{
Realm realm = Realm.GetInstance();
foreach (JObject data in jsonArray)
{
String customerId = data.GetValue("customerId").ToString();
Customer customer = realm.All<Customer>().Where(c => c.customerId == Java.Lang.Long.ParseLong(customerId)).FirstOrDefault();
if (customer != null)
{
String customerName = customer.customerName;
data.Add("customerName", customerName);
}
}
realm.Dispose();
var quotationsList = jsonArray.ToObject<IList<Quotation>>();
List<Quotation> quotation = new List<Quotation>(quotationsList);
return quotationsList;
}
catch(Exception e)
{
Debug.WriteLine(" exception "+e.StackTrace);
}
return null;
}
protected override Quotation parseObject(Type objectType, JObject jsonObject)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
这是JsonConverter
public abstract class JsonConverter<T> : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(JsonConverter<T>));
}
protected abstract T parseObject(Type objectType, JObject jsonObject);
protected abstract IList<T> parseArray(Type objectType, JArray jsonArray);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
var jsonArray = JArray.Load(reader);
var data= parseArray(objectType, jsonArray);
return data;
}
catch(Exception e)
{
Debug.WriteLine(e.StackTrace);
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Debug.WriteLine("Mo " + value);
}
}
the issue is when i am getting Response object inside that quotationsList is coming blank.
Run Code Online (Sandbox Code Playgroud)
Json从服务器收到
quotationsList: [
{
"account": null,
"contactId": 0,
"currency": "USD",
"customerId": 5637144583,
"deliveryAddress": "19TH and Edwardsville RD (RT203)\nGranite City,IL62040\nUSA",
"expiryDate": "2017-09-04",
"followUpDate": "2017-09-01",
"mQuotationId": null,
"opportunityId": 0,
"postedOn": null,
"prospectId": 0,
"quotationFor": null,
"quotationId": 5637155076,
"quotationName": "United States Steel",
"quotationNumber": "UST1-000022",
"quotationStatus": "Confirmed",
"requestReceiptDate": "2017-08-05",
"requestShipDate": "2017-08-05",
"siteId": "GLEN1",
"wareHouseId": "37"
}
Run Code Online (Sandbox Code Playgroud)
预计json
quotationsList: [
{
"account": null,
"contactId": 0,
"currency": "USD",
"customerId": 5637144583,
"deliveryAddress": "19TH and Edwardsville RD (RT203)\nGranite City,IL62040\nUSA",
"expiryDate": "2017-09-04",
"followUpDate": "2017-09-01",
"mQuotationId": null,
"opportunityId": 0,
"postedOn": null,
"prospectId": 0,
"quotationFor": null,
"quotationId": 5637155076,
"quotationName": "United States Steel",
"quotationNumber": "UST1-000022",
"quotationStatus": "Confirmed",
"requestReceiptDate": "2017-08-05",
"requestShipDate": "2017-08-05",
"siteId": "GLEN1",
"wareHouseId": "37",
"customerName":"Jhon Caro"
}
Run Code Online (Sandbox Code Playgroud)
报价模型
public class Quotation : RealmObject , IMaster, IMedia, IProduct
{
[PrimaryKey]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String mQuotationId { get; set; } = Guid.NewGuid().ToString();
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long quotationId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public String customerName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string quotationName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string quotationNumber{ get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string deliveryAddress { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string expiryDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string requestShipDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string requestReceiptDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long prospectId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string followUpDate { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public long opportunityId { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string postedOn { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
更新
protected override IList<Quotation> parseArray(Type objectType, JArray jsonArray)
{
try
{
Realm realm = Realm.GetInstance();
var data = jsonArray.ToObject<IList<Quotation>>();
List<Quotation> quotationList = new List<Quotation>(data);
foreach (Quotation quotation in quotationList)
{
long customerId = quotation.customerId;
Customer customer = realm.All<Customer>().Where(c => c.customerId == customerId).FirstOrDefault();
if (customer != null)
{
String customerName = customer.customerName;
quotation.customerName = customerName;
}
}
realm.Dispose();
return quotationList;
}
catch(Exception e)
{
Debug.WriteLine(" exception "+e.StackTrace);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
这就是我的反序列化被调用的方式
响应responseData = await Task.Run(()=> JsonConvert.DeserializeObject(content));
这里的问题不是 JSON 反序列化或添加到 json 数组,问题是您没有来自数据库的记录。
你正在使用这个:
long customerId = quotation.customerId;
Customer customer = realm.All<Customer>().Where(c => c.customerId == customerId).FirstOrDefault();
if (customer != null)
{
String customerName = customer.customerName;
quotation.customerName = customerName;
}
Run Code Online (Sandbox Code Playgroud)
FirstOrDefault 将期望查询返回零个或多个项目,但您只想访问代码中的第一个项目(即您不确定是否存在具有给定键的项目)
将其更改为First();并查看它是否抛出异常,如果抛出异常,那么这是您的问题和您的:
realm.All<Customer>().Where(c => c.customerId == customerId)
Run Code Online (Sandbox Code Playgroud)
返回不正确的数据。