0

I have the following JSON assigned to the variable strP:

{"get_the_data":[{"when_date":"09/12/2019","which_loc":"Orlando","who_witness":"visitor"}]}

And I need to generate the following output:

get_the_data:
    when_date - 09/12/2019
    which_loc - Orlando
    who_witness - visitor

How can I deserialize this JSON so as to get the KEY and the VALUE of each array within the object? Here is what I have tried so far:

string object1, string array1;
var jsonObj = new JavaScriptSerializer().Deserialize<RO>(strP);
//get the parent key: 'get_the_data'
object1 = get_the_data.ToString();
foreach (var p in strP._data)
{
    //how can I get the KEY and the VALUE of each array within the object
    array1 += p.Key + " - " + p.Value + Environment.NewLine; //e.g. when_date - 09/12/2019
}

Console.WriteLine(object1 + ":" + Environment.NewLine + array1);
//...
public class Data1
{
    public string when_date { get; set; }
    public string which_loc { get; set; }
    public string who_witness { get; set; }
}

public class RO
{
    public List<Data1> _data { get; set; }
}

p.s. I am wanting to avoid using external JSON library and use native C# methods.

dbc
  • 104,963
  • 20
  • 228
  • 340
Si8
  • 9,141
  • 22
  • 109
  • 221
  • 1
    Just out of curiosity, what do you have against using Json.net? It's incredibly mature and the number one downloaded package on Nuget. You're creating **so** much more work for yourself by avoiding it – Jesse Carter Jul 11 '17 at 19:48
  • Nothing really but wanted to do it natively... but I will check it out :) Thanks. – Si8 Jul 11 '17 at 19:59
  • 1
    I think for most .Net developers (and even internally at Microsoft) Json.net is considered the "native" json solution. It ships by default with a ton of Microsoft templates especially around asp.net – Jesse Carter Jul 11 '17 at 20:02
  • If I am already using native javascriptdeserializer by default and importing JSON.net break anything? – Si8 Jul 11 '17 at 20:20
  • No, it shouldn't break anything – Jesse Carter Jul 11 '17 at 20:48

1 Answers1

1

If you're just looking to get the keys and values from the JSON without hardcoding the key names in advance, you can deserialize to a Dictionary<string, List<Dictionary<string, string>>>:

var jsonObj = new JavaScriptSerializer().Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(strP);

string indent = "   ";
var sb = new StringBuilder();
foreach (var outerPair in jsonObj)
{
    sb.Append(outerPair.Key).AppendLine(":");
    outerPair.Value.SelectMany(d => d).Aggregate(sb, (s, p) => s.Append(indent).Append(p.Key).Append(" - ").AppendLine(p.Value));
}

Console.WriteLine(sb);

Incidentally, your RO type cannot be used to deserialize the JSON shown in your question because the name of its property:

public List<Data1> _data { get; set; }

differs from the property name in the JSON:

{"get_the_data":[ ... ] }

These property names need to match since JavaScriptSerializer does not have built-in support for renaming of properties during (de)serialization. See here for details.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • I like the LINQ method. Thanks. I will test and let you know how it went. – Si8 Jul 12 '17 at 00:54
  • I get this error: `Type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89],[System.Collections.Generic.List`1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89]]' is not supported for deserialization of an array.` – Si8 Jul 12 '17 at 19:33
  • @Si8 - that will happen if your actual JSON contains an array -- an ordered sequence of values surrounded by `[` and `]` -- rather than an object -- an unordered set of key/value pairs surrounded by `{` and `}`. Can you post your actual JSON? Is it more complex than the JSON you show? – dbc Jul 12 '17 at 19:49
  • https://jsfiddle.net/v9o91bm5/1/ The HTML is what I would like to retrieve from the Json. The key/value are both dynamic so I would like to be pulled from the Json itself. – Si8 Jul 13 '17 at 13:17
  • 1
    @Si8 - I just ran the code from the answer using your new example, and for output I got `samp_data: value_1 - 09/12/2019 value_2 - Leg value_3 - New York value_4 - 0000999201 value_5 - Side value_6 - Test value_7 - 540-090-3443`, which is what you want. There was no exception. If you can provide a [mcve] that doesn't work, I can try to help, because at this point the question seems answered. – dbc Jul 13 '17 at 18:53