-4

I got http result in JSON like that

{
   "success":true,
   "message":[
      
   ],
   "conversationList":[
      "dd184c5a-7eb7-4e98-b588-975853c51b18",
      "a3c25d14-d0d5-4e79-928b-1123fe1ba587",
      "8c5e2869-43f8-4555-9afd-3b1e679d5ed0",
      "843dcbd4-33fc-487f-9971-7fd80ff378c4",
      "62dcee64-aeb6-448d-9422-c6b94a22aa02",
      "409a8da6-bf32-4dee-923c-213a9971283c"
   ]
}

I make classes like that:

public class Root
{
    public bool success { get; set; }
    public List<object> message { get; set; }
    public List<string> conversationList { get; set; }
}

and deserialization like this:

var deserializedJson = JsonConvert.DeserializeObject<List<Root>>(result);

Then I got this error:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleApp9.Root]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'success', line 1, position 11.'

I want to use foreach on this and get all conversationList. How can I resolve this error?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
encoder93x
  • 1
  • 2
  • 4
  • 1
    Do you recieve this exact JSON? if so, then change `List` to `Root` – PawZaw Jan 04 '22 at 14:42
  • Change `var deserializedJson = JsonConvert.DeserializeObject>(result);` to `var deserializedJson = JsonConvert.DeserializeObject(result);` – Izzy Jan 04 '22 at 14:42
  • When I use this I get output: "ConsoleApp9.Root" – encoder93x Jan 04 '22 at 14:57
  • Are you calling `.ToString()` on `deserializedJson`? What code do you have that produces that output? We need more details. – Orion Jan 04 '22 at 15:00
  • This output I get from external API as http result. I calling `deserializedJson` and I try `deserializedJson.ToString()` result is the same. – encoder93x Jan 04 '22 at 16:27

1 Answers1

0

you don' t have a list in your root. Use this

var deserializedJson = JsonConvert.DeserializeObject<Root>(result);
Serge
  • 40,935
  • 4
  • 18
  • 45
  • When I use this I get output: "ConsoleApp9.Root" – encoder93x Jan 04 '22 at 14:58
  • I tested json you posted in Visual Studio. Everything is working properly. How you got json you posted? – Serge Jan 04 '22 at 15:09
  • This output I get from external API as http result. – encoder93x Jan 04 '22 at 18:23
  • @encoder93x if you have an error, it means that you have a different output. Could you check again pls? – Serge Jan 04 '22 at 18:25
  • Not error, output from deserializedJson = "ConsoleApp9.Root", should be "dd184c5a-7eb7-4e98-b588-975853c51b18"... – encoder93x Jan 04 '22 at 18:46
  • @encoder93x I am talking about your api response json string , I know what should deserialization result. – Serge Jan 04 '22 at 18:55
  • My HTTP result from external API is `{ "success":true, "message":[ ], "conversationList":[ "dd184c5a-7eb7-4e98-b588-975853c51b18", "a3c25d14-d0d5-4e79-928b-1123fe1ba587", "8c5e2869-43f8-4555-9afd-3b1e679d5ed0", "843dcbd4-33fc-487f-9971-7fd80ff378c4", "62dcee64-aeb6-448d-9422-c6b94a22aa02", "409a8da6-bf32-4dee-923c-213a9971283c" ] }` – encoder93x Jan 04 '22 at 18:57