sganesh73 Ответов: 3

Как удалить двойную кавычку из строки JSON.


вот моя строка JSON

финал1 = [{"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.

DynamicJsonCollection = JsonConvert.DeserializeObject & lt;List & lt;marketdata>> (final1);

Ошибка:

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: 


Что я уже пробовал:

Мы попробовали это сделать как показано ниже
DynamicJsonCollection = JsonConvert.DeserializeObject & lt;List & lt;marketdata>> (final1);

3 Ответов

Рейтинг:
2

Graeme_Grant

Ваш JSON недействителен. Так и должно быть:

[{"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}]}]

Что переводится как:
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; }
}


Рейтинг:
1

_Asif_

Есть проблемы в JSON-строку. Правильный json будет выглядеть так

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}]}]


Рейтинг:
0

Divya-Systematix

Вы можете попробовать вот это:

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();