问题
The strange thing is that it works fine on my local deploy (Win 10 IIS) but when I deploy it to our UAT server, it goes nuts.
I'm trying to deserialize the following json string:
{
"header": {
"application-code": "ZZZ"
},
"piece": [
"0000001"
]
}
The classes that represent this are defined:
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; }
}
Now, when I run this code:
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 is returning the following error:
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
Again, I run the same publish on my local IIS without a problem, so I'm completely stuck.
来源:https://stackoverflow.com/questions/50426850/c-sharp-newtonsoft-deserializeobject-error