简体   繁体   中英

How to fix Error while converting Json string to Object C#?

Im using C# to get a file from my local pc data folder.
This is the code to do that:

var _rootpath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + directory;
var ENC = new Encryption();
var s = File.ReadAllText(_rootpath + "json");
var x = ENC.RijndaelDecrypt(s, App.EncryptionPassword);

This works fine so far.
x got now this value (so this is the string I want to convert to an object) :

{  
"items":[  
   {  
      "id":194,
      "guid":"594394",
      "name":"Test",
      "connectorId":248,
      "customerId":1,
      "customerName":"company",
      "connectorTypeId":10,
      "connectorTypeIcon":null,
      "connectorCategoryId":1,
      "vendor":"FasterForward",
      "isActive":true,
      "shopId":null,
      "sku":null,
      "workerBearerToken":"",
      "workerUri":"http://localhost:9000"
   }
],
"responseStatus":null
}

After this I want to convert this to an object

var _response = JsonConvert.DeserializeObject<CrmJobListResponse>(x);

This line gives an error:

{"Error converting value x to type 'ServiceModel.CrmJobListResponse'. Path '', line 1, position 991."}

ServiceModel.CrmJobListResponse:

namespace ServiceModel
{
  public class CrmJobListResponse : ResponseBase
  {
   public CrmJobListResponse();

   public List<CrmJob> Items { get; set; }
  }
}

CrmJob class:

namespace ServiceModel.DTO
{
  public class CrmJob : IHasId<int>
  {
   public CrmJob();

   [Ignore]
   public string WorkerBearerToken { get; set; }
   [PropertyValue("sku")]
   public string SKU { get; set; }
   [PropertyValue("shop_id")]
   public string ShopId { get; set; }
   public bool IsActive { get; set; }
   public string Vendor { get; set; }
   public int ConnectorCategoryId { get; set; }
   [Ignore]
   public string WorkerRefreshToken { get; set; }
   public string ConnectorTypeIcon { get; set; }
   public string CustomerName { get; set; }
   public int CustomerId { get; set; }
   public int ConnectorId { get; set; }
   [PropertyValue("jobname")]
   public string Name { get; set; }
   public string Guid { get; set; }
   public int Id { get; set; }
   public int ConnectorTypeId { get; set; }
   [Ignore]
   public string WorkerUri { get; set; }
  }
}

Does anyone know why it can't convert my Json string to an object?
I didn't made the code myself, but I don't see why It should go wrong...

If you have a hard time creating DTOs you have some tools that may assist you https://app.quicktype.io/

You can also use paste special in VS to paste a Json directy to a C# class .

This also shows you if you malformed a Json.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM