locked
Newtonsoft Json -deserialize and add to dictionary object. RRS feed

  • Question

  • User-148788041 posted

    hi

    i have the following code. i want to add the deserialized json to dictionary like

    Dim ResponseFromServer As String = System.IO.File.ReadAllText("json1.json")
    Dim jsonResult = (Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(ResponseFromServer))
    Dictionary<string,string> str = new Dictionary<string,string>();
    str.Add("result",jsonResult); error here

    Error converting Jsonobject to string

    Thursday, August 23, 2018 5:28 PM

All replies

  • User475983607 posted

    hi

    i have the following code. i want to add the deserialized json to dictionary like

    Dim ResponseFromServer As String = System.IO.File.ReadAllText("json1.json")
    Dim jsonResult = (Newtonsoft.Json.JsonConvert.DeserializeObject(Of Rootobject)(ResponseFromServer))
    Dictionary<string,string> str = new Dictionary<string,string>();
    str.Add("result",jsonResult); error here
    
    

    Error converting Jsonobject to string

    Well, you copied this code form one of my responses and clearly have no idea what it does.  

    jsonResult is a .NET object Rootobject not a string.

    Thursday, August 23, 2018 5:39 PM
  • User753101303 posted

    Hi,

    Try perhaps https://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm or maybe you want a Dictionary<string,RootObject> ?

    Showing code that fails is not always enough to understand what is your exact intent. I'm not even sure if you want to use VB or C# ;-)

    Thursday, August 23, 2018 6:07 PM
  • User-148788041 posted
    Need in c#
    Friday, August 24, 2018 4:38 AM
  • User753101303 posted

    It told that because from the 4 lines you posted the first two are using VB.NET and the next ones C#.. If you "want to  add the deserialized json" (that is an object not a string) "to a dictionary" it should be:

    Dictionary<string,Rootobject> str = new Dictionary<string,Rootobject>();
    str.Add("result",jsonResult); error here

    I suspect that you may want instead to load each properties in a Dictionary<string,string> which is shown in the product documentation at :
    https://www.newtonsoft.com/json/help/html/DeserializeDictionary.htm 

    Friday, August 24, 2018 6:49 AM