1

I try to deserialize a json-string into object with Newtonsoft.Json. But JsonConvert.DeserializeObject() always returns null.

string json2 = "[{ 'id':1,'date':'2016-05-10T03:48:21','date_gmt':'2016-05-10T03:48:21','guid':{ 'rendered':'http://test.de/?p=1'},'modified':'2019-02-27T11:56:21'}]";

List<Product> myProducts = new List<Product>();

myProducts = JsonConvert.DeserializeObject<List<Product>>(json2); // allways null!?

I guess the reason lies in the class MyGuid. The setter of the property Rendered is never reached.

I have read all questions about this theme here but didn't find the right answer to my question.

Here is the whole code example:

namespace JsonToObject
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            string json2 = "[{ 'id':1,'date':'2016-05-10T03:48:21','date_gmt':'2016-05-10T03:48:21','guid':{ 'rendered':'http://test.de/?p=1'},'modified':'2019-02-27T11:56:21'}]";

            List<Product> myProducts = new List<Product>();

            myProducts = JsonConvert.DeserializeObject<List<Product>>(json2); // allways null!
        }
    }

    public class Product
    {
        [JsonProperty("id")]
        public int Id { get; set; }

        [JsonProperty("date")]
        public string Date { get; set; }

        [JsonProperty("date_gmt")]
        public string Date_gmt { get; set; }

        [JsonProperty("guid")]
        public MyGuid MyGuid { get; set; }

        [JsonProperty("modified")]
        public string Modified { get; set; }
    }


    public class MyGuid
    {
        [JsonProperty("rendered")]
        public string Rendered { get; set; } // not reached!
    }

}

Can anyone help me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
insum
  • 11
  • 4
  • to start with, single quotes are not valid delimiters in JSON, although I don't know if json.net strictly enforces that. – Jason Mar 14 '19 at 20:51
  • 1
    Your code works fine on my end. I managed to deserialize one Product object with all of its Properties being not null. I would recomment to try the other route. Create a Product instance with the respective Properties, try to serialize it and compare the result to your initial JSON string. good luck – Mouse On Mars Mar 14 '19 at 21:55
  • Are you using the xamarin live player? If so, Json.NET serializer apparently doesn't work there, see [JsonConvert.SerializeObject always return {} in XamarinForms](https://stackoverflow.com/q/48041823) and [Newtonsoft.Json deserialize object in Xamarin iOS project](https://stackoverflow.com/q/47379055). – dbc Mar 14 '19 at 22:46
  • Your code is no problem.If you check myProducts directly, then it is an array object, you need to specify which element in the array, which property can get Rendered.Since your json array contains only one object, getting Rendered can be written like this:`myProducts[0].MyGuid.Rendered` .Then this will return : `http://test.de/?p=1`. – Junior Jiang Mar 15 '19 at 02:30
  • @dbc: Thank you for your answer and the links. Yes, I'm using xamarin live player for an Android project. In a "normal" WPF-project the code works fine. – insum Mar 15 '19 at 15:52
  • @Michael C. I have tried the other route: ` MyGuid mgid = new MyGuid(); mgid.Rendered = "http://test.de/?p=1"; Product myProd = new Product(); myProd.Date = "2016-05-10T03:48:21"; myProd.Id = 1; myProd.Date_gmt = "2016-05-10T03:48:21"; myProd.MyGuid = mgid; myProd.Modified = "2019-02-27T11:56:21"; string json = JsonConvert.SerializeObject(myProd); ` Unfortunately json = "{}". I think the problem lies in fact in the xamarin live player in combination with json.NET. – insum Mar 15 '19 at 16:16
  • In that case, do the solutions shown in [Newtonsoft.Json deserialize object in Xamarin iOS project](https://stackoverflow.com/q/47379055) work for you, either 1) Using `JToken.Parse` and manually constructing your `Product` list, or 2) Setting iOS Build Linker Behavior to "Link Framework SDKs only"? – dbc Mar 15 '19 at 16:45

1 Answers1

-1

But JsonConvert.DeserializeObject() always returns null.

Your code is no problem.Normally this return an json array object.

enter image description here

If you check myProducts directly, then it is an array object, you need to specify which element in the array, which property can get Rendered.

Since your json array contains only one object, getting Rendered can be written like this:

myProducts[0].MyGuid.Rendered

Then this will return :

http://test.de/?p=1

All parameters are get as follow:

myProducts//------ System.Collections.Generic.List`1[App1.Views.MainPage+Product]
myProducts[0].Id //------1
myProducts[0].Date//------2016-05-10T03:48:21
myProducts[0].Date_gmt//------2016-05-10T03:48:21
myProducts[0].Modified//------2019-02-27T11:56:21
myProducts[0].MyGuid.Rendered//------http://test.de/?p=1

If also has problem, you can share link of solution.I will check it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • 1
    In a "normal" Visual Studio project (WPF, console,...) the code works fine. I think the problem lies in fact in the xamarin live player in combination with json.NET. – insum Mar 15 '19 at 16:19
  • @insum Yeah, it may be the reason.You can share link about sample project. – Junior Jiang Mar 18 '19 at 01:26