C#NewtonSoft DeserializeObject错误

时间:2018-05-19 15:29:58

标签: c# json object serialization json.net

奇怪的是,它在我的本地部署(Win 10 IIS)上工作正常但是当我将它部署到我们的UAT服务器时,它很疯狂。

我正在尝试反序列化以下json字符串:

{
  "header": {
    "application-code": "ZZZ"
  },
  "piece": [
    "0000001"
  ]
}

定义了代表它的类:

public class RequestPiece
{
    [JsonProperty(PropertyName = "header")]
    public RequestHeaderPiece requestheader { get; set; }
}

public class ResponseHeaderPiece
{
    [JsonProperty(PropertyName = "application-code")]
    public string application_code { get; set; }
}

public class ResponsePiece
{
    [JsonProperty(PropertyName = "header")]
    public ResponseHeaderPiece responseheader { get; set; }
    [JsonProperty(PropertyName = "piece")]
    public List<string> piece { get; set; }
}

现在,当我运行此代码时:

        public List<string> getP(string pieces)
    {
        try{
            restAPI.Url = config.PieceServiceUrl + "/" + pieces;
            restAPI.Method = "GET";
            restAPI.ClientId = config.ClientId;
            restAPI.Secret = config.SecretKey;
            string returnString = restAPI.callJsonService();
            log.Info(System.Reflection.MethodInfo.GetCurrentMethod() + "::JSON response  => " + returnString);
            ResponsePiece rp = JsonConvert.DeserializeObject<ResponsePiece>(returnString);
            log.Info(System.Reflection.MethodInfo.GetCurrentMethod() + "::JSON response " + rp.piece);
            return rp.piece;
        }
        catch (Exception ex)
        {
            log.Error(System.Reflection.MethodInfo.GetCurrentMethod() + "::failed::" + ex.Message.ToString() + " => " + ex.StackTrace.ToString());
            return null;
        }
    }

JsonConvert.DeserializeObject返回以下错误:

   RequestPid System.Collections.Generic.List`1[System.String] getP(System.String)::failed::System.Reflection.DefaultMemberAttribute.m_memberName =>
   at System.Reflection.RtFieldInfo.PerformVisibilityCheckOnField(IntPtr field, Object target, IntPtr declaringType, FieldAttributes attr, UInt32 invocationFlags)
   at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency)
   at System.Reflection.RtFieldInfo.GetValue(Object obj)
   at System.Attribute.GetHashCode()
   at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
   at System.Linq.Set`1.InternalGetHashCode(TElement value)
   at System.Linq.Set`1.Find(TElement value, Boolean add)
   at System.Linq.Set`1.Add(TElement value)
   at System.Linq.Enumerable.<UnionIterator>d__81`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Newtonsoft.Json.Utilities.ReflectionUtils.GetAttributes(Object attributeProvider, Type attributeType, Boolean inherit)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociateMetadataTypeFromAttribute(Type type)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAssociatedMetadataType(Type type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Type type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetAttribute[T](Object provider)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.CachedAttributeGetter`1.GetAttribute(Object type)
   at Newtonsoft.Json.Serialization.JsonTypeReflector.GetCachedAttribute[T](Object attributeProvider)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreatePrimitiveContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at code.RequestP.getP(String pieces) in RequestP.cs:line 51

同样,我在本地IIS上运行相同的发布没有问题,所以我完全陷入困境。

0 个答案:

没有答案