如何从JSON字符串中删除双引号。 [英] How do remove the double quote from the JSON string.
问题描述
这里是我的JSON字符串
final1 = [{Col1:219,Col2:234,Col3:79 COL4: 12, COL5: 0D2, COL6: 14, COL7: 35, Col8: 空, NUMB: [{ PPWK : 201710\" , 卷:0},{ PPWK: 201711, 卷:0},{ PPWK: 201712, 卷:0},{ PPWK: 201713,Vol:0}]}]
我们在反序列化JSON字符串时收到错误。
DynamicJsonCollection = JsonConvert.DeserializeObject< List< marketdata>>(final1);
错误:
用户代码未处理Newtonsoft.Json.JsonSerializationException
HResult = -2146233088
消息=将值[{转换为'System.Collections'时出错.Generic.List`1 [WPFJSON.Numb]。路径'[0] .NUMB',第1行,位置105.
Source = Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader,Object value) ,CultureInfo culture,JsonContract contract,Type targetType)
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属性,JsonConverter propertyConverter,JsonContainerContract containerContract,JsonProperty containerProperty,JsonReader reader,Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject,JsonReader reader, JsonObjectContract合约,JsonProperty成员,String id)Newton的
soft.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.PopulateList(IList list,JsonReader reader,JsonArrayContract contract,JsonProperty containerProperty,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader,Type objectType,JsonContract contract,JsonProperty member,Object existingValue,String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader re ader,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.JsonSerializer.Deserialize(JsonReader reader,Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value,Type type ,JsonSerializerSettings设置)
at Newtonsoft.Json.JsonConvert.DeserializeObject [T](字符串值,JsonSerializerSettings设置)
at Newtonsoft.Json.JsonConvert.DeserializeObject [T](String value)
at c:\ Users\gsubram2 \Documents中的WPFJSON.ViewModel..ctor()#Visual Studio 2012 \Projects\WPFJSON\WPFJSON \ViewModel.cs:第67行
在WPFJSON.MainWindow..ctor()in c:\ Users\gsubram2 \Documents\Visual Studio 2012 \Projects\WPFJSON\WPFJSON\MainWindow.xaml.cs:line 34
InnerException :System.ArgumentException
HResult = -2147024809
Message =无法从System.String转换或转换为System.Collections.Generic.List`1 [WPFJSON.Numb]。
Source = Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value,Type initialType,Type targetType)
at Newtonsoft.Json.Utilities。 ConvertUtils.ConvertOrCast(Object initialValue,CultureInfo culture,Type targetType)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader,Object value,CultureInfo culture,JsonContract contract,Type targetType)
InnerException:
我尝试过:
我们已经尝试过如下
DynamicJsonCollection = JsonConvert.DeserializeObject< List< marketdata>>(final1);
你的JSON是无效。它应该是:
[{Col1:219,Col2:234,Col3:79,Col4: 12, COL5: 0D2, COL6: 14, COL7: 35, Col8: 空, NUMB:[{ PPWK: 201710, 卷:0},{ PPWK: 201711, 卷:0},{ PPWK: 201712, 卷:0},{ PPWK: 201713,卷:0}]}]
转换为:
public class NUMB
{
[JsonProperty( PPWK)]
public 字符串 PPWK { get ; set ; }
[JsonProperty( Vol)]
< span class =code-keyword> public int Vol { get ; set ; }
}
public class 数据
{
[JsonProperty( Col1)]
public string Col1 { get ; set ; }
[JsonProperty( Col2)]
< span class =code-keyword> public string Col2 { get ; set ; }
[JsonProperty( Col3)]
< span class =code-keyword> public string Col3 { get ; set ; }
[JsonProperty( Col4)]
< span class =code-keyword> public string Col4 { get ; set ; }
[JsonProperty( Col5)]
< span class =code-keyword> public string Col5 { get ; set ; }
[JsonProperty( Col6)]
< span class =code-keyword> public string Col6 { get ; set ; }
[JsonProperty( Col7)]
< span class =code-keyword> public string Col7 { get ; set ; }
[JsonProperty( Col8)]
< span class =code-keyword> public string Col8 { get ; set ; }
[JsonProperty( NUMB)]
< span class =code-keyword> public IList< NUMB> NUMB {获取; set ; }
}
你可以尝试这个:
var responseData = responseMessage.Content .ReadAsStringAsync()结果。
var objMarketData = JsonConvert.DeserializeObject< List< marketdata>>(responseData);
和json中的get json值:
var responseData = responseMessage.Content.ReadAsStringAsync()。Result;
var jsonData = JObject.Parse(responseData);
var numb = jsonData [NUMB]。ToString();
json字符串中存在问题。正确的json会是这样的
final = [{ < span class =code-string> Col1: 219, Col2: 234, Col3: 79, Col4: 12, Col5: 0D2, Col6: 14, Col7: 35, Col8: null , NUMB:[{ PPWK: 201710, Vol: 0 },{ PPWK: 201711, Vol: 0 },{ PPWK: 201712, Vol: 0 },{ PPWK: 201713, Vol: 0 } ]}]
here my JSON string
final1 = [{"Col1":"219","Col2":"234","Col3":"79","Col4":"12","Col5":"0D2","Col6":"14","Col7":"35","Col8":"null","NUMB":"[{"PPWK":"201710","Vol":0},{"PPWK":"201711","Vol":0},{"PPWK":"201712","Vol":0},{"PPWK":"201713","Vol":0}]"}]
We are getting error when do Deserialise the JSON string.
DynamicJsonCollection = JsonConvert.DeserializeObject<List<marketdata>>(final1);
Error:
Newtonsoft.Json.JsonSerializationException was unhandled by user code HResult=-2146233088 Message=Error converting value "[{" to type 'System.Collections.Generic.List`1[WPFJSON.Numb]'. Path '[0].NUMB', line 1, position 105. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) 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.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) 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.JsonSerializer.Deserialize(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 WPFJSON.ViewModel..ctor() in c:\Users\gsubram2\Documents\Visual Studio 2012\Projects\WPFJSON\WPFJSON\ViewModel.cs:line 67 at WPFJSON.MainWindow..ctor() in c:\Users\gsubram2\Documents\Visual Studio 2012\Projects\WPFJSON\WPFJSON\MainWindow.xaml.cs:line 34 InnerException: System.ArgumentException HResult=-2147024809 Message=Could not cast or convert from System.String to System.Collections.Generic.List`1[WPFJSON.Numb]. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) InnerException:
What I have tried:
We have tried it as below
DynamicJsonCollection = JsonConvert.DeserializeObject<List<marketdata>>(final1);
Your JSON is invalid. It should be:
[{"Col1":"219","Col2":"234","Col3":"79","Col4":"12","Col5":"0D2","Col6":"14","Col7":"35","Col8":"null","NUMB":[{"PPWK":"201710","Vol":0},{"PPWK":"201711","Vol":0},{"PPWK":"201712","Vol":0},{"PPWK":"201713","Vol":0}]}]
Which translates to:
public class NUMB { [JsonProperty("PPWK")] public string PPWK { get; set; } [JsonProperty("Vol")] public int Vol { get; set; } } public class Data { [JsonProperty("Col1")] public string Col1 { get; set; } [JsonProperty("Col2")] public string Col2 { get; set; } [JsonProperty("Col3")] public string Col3 { get; set; } [JsonProperty("Col4")] public string Col4 { get; set; } [JsonProperty("Col5")] public string Col5 { get; set; } [JsonProperty("Col6")] public string Col6 { get; set; } [JsonProperty("Col7")] public string Col7 { get; set; } [JsonProperty("Col8")] public string Col8 { get; set; } [JsonProperty("NUMB")] public IList<NUMB> NUMB { get; set; } }
You can try this one:
var responseData = responseMessage.Content.ReadAsStringAsync().Result; var objMarketData = JsonConvert.DeserializeObject<List<marketdata>>(responseData); and for get json value in the json : var responseData = responseMessage.Content.ReadAsStringAsync().Result; var jsonData = JObject.Parse(responseData); var numb= jsonData["NUMB"].ToString();
There are problems in your json string. Correct json would be like this
final = [{"Col1":"219","Col2":"234","Col3":"79","Col4":"12","Col5":"0D2","Col6":"14","Col7":"35","Col8":"null","NUMB":[{"PPWK":"201710","Vol":0},{"PPWK":"201711","Vol":0},{"PPWK":"201712","Vol":0},{"PPWK":"201713","Vol":0}]}]
这篇关于如何从JSON字符串中删除双引号。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!