0
public static string DeserializeNames()
{
    // Json I am passing for the deserialization.
    JsonStream= "{
    "head": {

        "Rows": [
            "test 1",
            [
                [
                    {
                        "@Key": "Domain",
                        "@value": "LocalHost"
                    },
                    {
                        "@Key": "Cookie name(s)",
                        "@value": "Google"
                    },
                    {
                        "@Key": "Purpose",
                        "@value": "Test"
                    },
                    {
                        "@Key": "lifetime",
                        "@value": "test"
                    }
                ]
                ]
]
}
}"


        //deserialize JSON from file  
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        var cookieList = serializer.Deserialize<List<Cookie>>(JsonStream).ToList();
}

//Class descriptions //I have created below classes for the deserialization. Records are not deserialized i am getting zero record count.

public class Categorization
        {
            public string categorizationName { get; set; }
            public List<object> KeyValue{ get; set; }
        }
        public class Head
        {
            public IList<Categorization> Rows { get; set; }
        }

        public class Cookie
        {
            public Head head { get; set; }
        }

Also created below set of the classes and tried the deserialization, Still no luck

public class Head 
{
    public List<object> Rows { get; set; }
}

public class Cookie
{
    public Head head { get; set; }
}

I am getting count as 0 i am not able to fetch any record.

Please help !!

Trilok Pathak
  • 2,931
  • 4
  • 28
  • 34
  • 1
    are you able to change that json since it won't work out of the box, you'll need to implement custom type converter for that – Darjan Bogdan Apr 13 '18 at 06:26
  • As Darjan says, you'll need to write your own converter for that as string and array are dissimilar types. – ProgrammingLlama Apr 13 '18 at 06:30
  • @DarjanBogdan I won't able to change the JSON , How to implement the custom type is also a quest for me. – Trilok Pathak Apr 13 '18 at 06:38
  • 1
    @Developer https://stackoverflow.com/questions/1341719/custom-javascriptconverter-for-datetime you can find here example of custom converter implementation and registration – Darjan Bogdan Apr 13 '18 at 07:07
  • 1
    Be aware that you should post real code, the code in your question doesn't compile because the string syntax is incorrect, this makes it hard for people to test your code in order to help you. – Lasse V. Karlsen Apr 13 '18 at 07:35
  • @LasseVågsætherKarlsen Updated the question. This should work. – Trilok Pathak Apr 17 '18 at 12:55

1 Answers1

0

I have modified the Json as below and stored in in the file "test.json" :

  {
    "head": {

            "TestRows": [

                [
                    {
                        "Key": "Domain",
                        "value": "Google"
                    },
                    {
                        "Key": "Cookie",
                        "value": "Test"
                    },
                    {
                        "Key": "Use for",
                        "value": "Test."
                    },
                    {
                        "Key": "Expire Time",
                        "value": "1 hour"
                    }
                  ]
                ]
    }
}

And created below set of classes :

public class Head
        {
            public IList<List<Dictionary<string, object>>> TestRows{ get; set; }
        }

        public class Cookie
        {
            public Head Head { get; set; }
        }




var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var path= Path.Combine(baseDirectory, "test.json");

            //deserialize JSON from file  
            string JsonStream = System.IO.File.ReadAllText(path, Encoding.Default);

            var DeserializedCookieList = JsonConvert.DeserializeObject<Cookie>(JsonStream);

Deserialization is working properly.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Trilok Pathak
  • 2,931
  • 4
  • 28
  • 34
  • @Darjan Bogdan , @ john ,@Lasse Vågsæther Karlsen If you can help me on this. – Trilok Pathak Apr 17 '18 at 12:54
  • _"I have another doubt"_ - then read [ask] and ask a new question. If you're trying to display links as links, then you need to ... display them as links and not as plaintext. It all depends on how you write your output. – CodeCaster Apr 17 '18 at 13:05