解析值时遇到意外字符:c。路径 , 行 0, 位置 0



当它从Visual Studio 正常运行时,我在应用程序中实现了一种方法,但是当它从任务计划程序运行时,在日志文件中收到此错误


System.Exception:在获取供应商列表和方法名称为 GetSuppliers 时,同步过程中发生异常。异常是 Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:C. 第 0 行,位置 0。at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter, Boolean inArray) at Newtonsoft.Json.Serialization

我的方法

public string GetSuppliers()
{
SqlConnection connect = null; var spdb = new Syncdbsource();
try
{
var hsp = new hspservice.MyServiceSoapClient("MyServiceSoap");
var sdetails = hsp.GetSupplier();
List<SupplierDetails> supplierslist = null; var countSuppliers = 0;
if (!string.IsNullOrEmpty(sdetails))
{ supplierslist = JsonConvert.DeserializeObject<List<SupplierDetails>>(sdetails); }
return countSuppliers + "Inserted";
}
catch (SocketException)
{
System.Threading.Thread.Sleep(25000);
try
{
var output = GetSuppliers();
return output;
}
catch (Exception ex)
{
var exception = new Exception(@"Exception occurred in the Sync Process while fetching the suppliers list and method name is GetSuppliers.
The Exception is " + Environment.NewLine + ex.Message);
var message = exception.Message;
SendMail.InsertIssue(message.Replace("'", "''"));
return "-1";
}
}
catch (Exception excp)
{

var exception = new Exception(@"Exception occurred in the Sync Process while fetching the suppliers list and method name is GetSuppliers.
The Exception is " + Environment.NewLine + excp);

SendMail.InsertIssue(exception.ToString().Replace("'", "''"));
}
finally
{
if (connect != null && connect.State == ConnectionState.Open)
{ connect.Dispose(); }
}
return string.Empty;
}

Json 返回字符串

[{"SUPPLIERID":"10148 ","供应商名称":"联合电子 - LUD ","TNAMC":"应收账款","TNAMB":" ","TNAME":">

沃思堡","TPSTC":"76113-2325","TCSTE":"TX","TCCTY":"美国","电话":"616-365-9960 ","传真":"6163659895 "},{"供应商ID":"10159 ","供应商名称":"阿罗钢铁公司","TNMC":"邮政信箱30382 ","TNAMB":" ","TNAME":"兰辛 ","TPSTC":"48909-7882","TCSTE":"MI","TCCTY":"USA","PHONE":"616-248-7687 ","FAX":"6164522779 "}]

public class SupplierDetails
{
private string _supplierCode;
private string _supplierName;
private string _internalCompanyId;
private string _address1;
private string _address2;
private string _city;
private string _state;
private string _country;
private string _zipCode;
private string _fax;
private string _phone;
public string SUPPLIERID
{
get {
return !string.IsNullOrEmpty(_supplierCode) ? _supplierCode : "-";
}
set
{
_supplierCode = value.Trim();
}
}
public string SUPPLIERNAME
{
get {
return !string.IsNullOrEmpty(_supplierName) ? _supplierName : "-";
}
set
{
_supplierName = value.Trim();
}
}
public string CompanyID
{
get {
return !string.IsNullOrEmpty(_internalCompanyId) ? _internalCompanyId : "-";
}
set
{
_internalCompanyId = value.Trim();
}
}

public string TNAMC
{
get {
return !string.IsNullOrEmpty(_address1) ? _address1 : "-";
}
set
{
_address1 = value.Trim();
}
}
public string TNAMB
{
get {
return !string.IsNullOrEmpty(_address2) ? _address2 : "-";
}
set
{
_address2 = value.Trim();
}
}
public string TNAME
{
get {
return !string.IsNullOrEmpty(_city) ? _city : "-";
}
set
{
_city = value.Trim();
}
}
public string TCSTE
{
get {
return !string.IsNullOrEmpty(_state) ? _state : "-";
}
set
{
_state = value.Trim();
}
}
public string TCCTY
{
get {
return !string.IsNullOrEmpty(_country) ? _country : "-";
}
set
{
_country = value.Trim();
}
}
public string TPSTC
{
get {
return !string.IsNullOrEmpty(_zipCode) ? _zipCode : "-";
}
set
{
_zipCode = value.Trim();
}
}
public string PHONE
{
get
{
return !string.IsNullOrEmpty(_phone) ? _phone : "-";
}
set
{
_phone = value.Trim();
}
}
public string FAX
{
get
{
return !string.IsNullOrEmpty(_fax) ? _fax : "-";
}
set
{
_fax = value.Trim();
}
}
public SupplierDetails()
{
//
// TODO: Add constructor logic here
//
}

}

我把你提出的最简单的实现放在一起,发现这有效:

string sdetails = "[{"SUPPLIERID":"10148 ","SUPPLIERNAME":"ALLIED ELECTRONICS -LUD ","TNAMC":"ACCOUNTS RECEIVABLE ","TNAMB":" ","TNAME":"FORT WORTH ","TPSTC":"76113 - 2325","TCSTE":"TX","TCCTY":"USA","PHONE":"616 - 365 - 9960 ","FAX":"6163659895 "},{"SUPPLIERID":"10159 ","SUPPLIERNAME":"ALRO STEEL CO. ","TNAMC":"P.O.BOX 30382 ","TNAMB":" ","TNAME":"LANSING ","TPSTC":"48909 - 7882","TCSTE":"MI","TCCTY":"USA","PHONE":"616 - 248 - 7687 ","FAX":"6164522779 "}]";
List<SupplierDetails> supplierslist = JsonConvert.DeserializeObject<List<SupplierDetails>>(sdetails);

您确定您在问题中列出的 JSON 字符串是您在sdetails中的完整响应吗? 鉴于错误指出您在字符 0 处有错误,我怀疑您没有收到您描述的 JSON。

相关内容

  • 没有找到相关文章