0

I am having a difficulty trying to extract data from the following JSON table:

    [
    {"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin"},
    {"type":"database","name":"archaism_dictionary"},
    {"type":"table","name":"dictionary","database":"archaism_dictionary","data":
    [
    {"id":"0","word":"wordOne","synonym":null,"definition":"defOne"},
    {"id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"}
    ]
    }
    ]

My goal is to get a string output for each "word" and each "definition". I have the following class which corresponds to the JSON file:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string type { get; set; }
    public string version { get; set; }
    public string comment { get; set; }
    public string name { get; set; }
    public string database { get; set; }
    public Datum[] data { get; set; }
}

public class Datum
{
    public string id { get; set; }
    public string word { get; set; }
    public object synonym { get; set; }
    public string definition { get; set; }
}

Finally this piece of code is supposed to retrieve the first word from the table in the string result:

var list = JsonConvert.DeserializeObject<List<Dictionary.Rootobject>>(rawJSON);
string result = list[0].Property1[0].data[0].word;

The .Property[0] returns null and the program gives me a null reference exception. Where is my code faulty and how should I accomplish this task? Thank you.

EDIT: I am not sure whether this can mess up the rawJSON string but I get it like this:

rawJSON = File.ReadAllText(FileSystem.AppDataDirectory + fileName);
AR Learn
  • 27
  • 4
  • 1
    Could you please provide a complete valid json content? Otherwise, it would be difficult to find out the problem. – Md Hasan Ibrahim Feb 25 '20 at 20:35
  • 1
    the first two elements in your data do not have a "data" property. Have you actually examined the deserialized json? – Jason Feb 25 '20 at 20:36
  • you can copy your json and Paste Special in Visual Studio to create appropriate classes or use www.json2csharp.com site to see what classes you need to properly deserialize your json – Jawad Feb 25 '20 at 20:39
  • 1
    Please review [MCVE] guide on posting code and [edit] post accordingly. In particular make sure to provide exact (but minimal) JSON that demonstrates the problem. Also since it is NRE make sure to review https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. – Alexei Levenkov Feb 25 '20 at 20:52
  • I'd love to help, but without a valid json example, I can't. – Casey Crookston Feb 25 '20 at 21:25
  • Hello all, thank you for replying. I am now using the first JSON, which Claudio provided. – AR Learn Feb 25 '20 at 23:40

3 Answers3

0

It is more likely that your input json is something like this:

[
{"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin"},
{"type":"database","name":"archaism_dictionary"},
{"type":"table","name":"dictionary","database":"archaism_dictionary","data":
[
{"id":"0","word":"wordOne","synonym":null,"definition":"defOne"},
{"id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"}
]

Note square brakets, they're important.

If I guessed it right, you'll want to deserialize like this:

var list = JsonConvert.DeserializeObject<List<Class1>>(rawJSON);
string result = list[2].data[0].word;

Note: your original code would work only if your input json were:

{
  "Property1":[
    {"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin"},
    {"type":"database","name":"archaism_dictionary"},
    {"type":"table","name":"dictionary","database":"archaism_dictionary","data":
      [
        {"id":"0","word":"wordOne","synonym":null,"definition":"defOne"},
        {"id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"}
      ]
    }
  ]
}

and use

var myRoot = JsonConvert.DeserializeObject<RootObject>(rawJSON);
string result = myRoot.Property1[2].data[0].word;
Claudio Valerio
  • 2,069
  • 13
  • 21
  • Thank you, I replaced the JSON with yours (the first one) and the command to yours but I still get the null as a returning value. – AR Learn Feb 25 '20 at 23:45
  • Had a typo in the indexer, it is list[2] instead of list[0]. I edited the answer and added code for secpnd json. Give it a try. – Claudio Valerio Feb 26 '20 at 05:01
0

@ Claudio Valerio provide the right json data.

Based on my test, you could try the code below to get the word in the list.

Json daya:

{
"Property1":[
{"type":"header","version":"4.8.3","comment":"Export to JSON plugin for PHPMyAdmin"},
{"type":"database","name":"archaism_dictionary"},
{"type":"table","name":"dictionary","database":"archaism_dictionary","data":
  [
    {"id":"0","word":"wordOne","synonym":null,"definition":"defOne"},
    {"id":"1","word":"wortTwo","synonym":null,"definition":"defTwo"}
  ]
  }
 ]
}

Class from JSON data:

 public class Rootobject
{
    public Property1[] Property1 { get; set; }
}

public class Property1
{
    public string type { get; set; }
    public string version { get; set; }
    public string comment { get; set; }
    public string name { get; set; }
    public string database { get; set; }
    public Datum[] data { get; set; }
}

public class Datum
{
    public string id { get; set; }
    public string word { get; set; }
    public object synonym { get; set; }
    public string definition { get; set; }
}

Code:

  string rawJSON = @"{
  'Property1':[
    {'type':'header','version':'4.8.3','comment':'Export to JSON plugin for PHPMyAdmin'},
   {'type':'database','name':'archaism_dictionary'},
   {'type':'table','name':'dictionary','database':'archaism_dictionary','data':
   [
    {'id':'0','word':'wordOne','synonym':null,'definition':'defOne'},
     {'id':'1','word':'wortTwo','synonym':null,'definition':'defTwo'}
  ]
  }
 ]
}";
        var list = JsonConvert.DeserializeObject<Rootobject>(rawJSON);
        string result = list.Property1[2].data[0].word;

enter image description here

Wendy Zang - MSFT
  • 6,190
  • 1
  • 3
  • 12
-1

You need to null handle(json NullValueHandling) below is my code please take a look :

string stringJson = @"{
                              'Property1':[
                                {'type':'header','version':'4.8.3','comment':'Export to JSON plugin for PHPMyAdmin'},
                               {'type':'database','name':'archaism_dictionary'},
                               {'type':'table','name':'dictionary','database':'archaism_dictionary','data':
                               [
                                {'id':'0','word':'wordOne','synonym':null,'definition':'defOne'},
                                 {'id':'1','word':'wortTwo','synonym':null,'definition':'defTwo'}
                              ]
                              }
                             ]
                            }";

        try
        {
            var settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore
            };
            var list = JsonConvert.DeserializeObject<BaseResponse>(stringJson,settings);
            string result = list.Property1[2].data[0].word;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

Models :

public class WordData
{
    public string id { get; set; }
    public string word { get; set; }
    public object synonym { get; set; }
    public string definition { get; set; }
}

public class PropertyData
{
    public string type { get; set; }
    public string version { get; set; }
    public string comment { get; set; }
    public string name { get; set; }
    public string database { get; set; }
    public List<WordData> data { get; set; }
}

public class BaseResponse
{
    public List<PropertyData> Property1 { get; set; }
}

I hope it will help you

Thanks