0

This is my JSON string who represent just a simple List: {"accelerationsList":"[-3.1769, 3.304, 6.3455997, 3.1701]"}

And this is my C# code to deserialize it:

HttpContent requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result; // i know about deadlock...
List<float> accelerationsList = new JavaScriptSerializer().Deserialize<List<float>>(jsonContent);

I dont know why my accelerationsList is empty! Any suggestions?

Iutho
  • 95
  • 8
  • 3
    Probably because it's a string in the JSON, not a list of floats. Also, you need a type that represents the JSON obejct. Have a look [here](https://stackoverflow.com/questions/5502245/deserializing-a-json-file-with-javascriptserializer) – Biesi Dec 07 '19 at 16:53
  • 1
    Is that actually your JSON? Is the array actually a string? Or is it really an array? – haldo Dec 07 '19 at 16:57
  • yes, this is my JSON and accelerationsList it come from my Java code: ```List accelerationsList = new ArrayList<>();``` – Iutho Dec 07 '19 at 17:08
  • your json is not an array of float, it's an object with a property called `accelerationsList` which has a string value, the content of which looks like a json array of float. – ESG Dec 07 '19 at 17:21

2 Answers2

1

Use Newtonsoft.Json, it makes it clean

string accelerationsListString = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonContent)["accelerationsList"];
List<float> accelerationsList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<float>>(accelerationsListString);

you get the list of float in a string, so we need to convert the string to list after extracting

Vignesh Prasad V
  • 419
  • 3
  • 17
  • Newtonsoft.Json.JsonSerializationException: 'Error converting value "[-3.6553, -3.1338, -3.1062]" to type 'System.Collections.Generic.List`1[System.Single]'. Path 'accelerationsList', line 1, position 50.' Inner Exception ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List`1[System.Single]. – Iutho Dec 07 '19 at 17:19
  • you get the list of float in a string, so we need to convert the string to the list after extracting. this will work – Vignesh Prasad V Dec 07 '19 at 17:35
  • I think yuo got it, thank you! Now I need to do the same but for a List so maybe se you soon ahah – Iutho Dec 07 '19 at 17:38
1

I believe you have most of what you are looking for except for 1 issue.

you are trying to convert the entire content (json string) to List. You need to convert the json object properly to get the value of accelerationsList, then convert the string that is your List of Floats properly.

string jsonContent = @"{""accelerationsList"":""[-3.1769, 3.304, 6.3455997, 3.1701]""}";
var stringRepOfArray = JObject.Parse(jsonContent)["accelerationsList"].ToString();

List<float> floatList = new JavaScriptSerializer().Deserialize<List<float>>(stringRepOfArray);

Output:

floatList
Count = 4
    [0]: -3.1769
    [1]: 3.304
    [2]: 6.34559965
    [3]: 3.1701
Jawad
  • 11,028
  • 3
  • 24
  • 37