Click here to Skip to main content
15,916,042 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted
Updated 27-Mar-17 20:56pm

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:
C#
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; }
}
 
Share this answer
 
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();
 
Share this answer
 
There are problems in your json string. Correct json would be like this

C#
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}]}]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900