-1

I have a JSON that looks like this:

{ 
    "Identifier1":"TextOfIdentifier1",
    "Identifier2":"TextOfIdentifier2",
    "Identifier3":"TextOfIdentifier3",
    ...
}

I know how to deseralize a JSON into a custom object, I followed what says here, but all identifiers appear in the same JSON tag...

How can I get all identifiers inside the JSON?

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131
  • The first option in linked answer is ["LINQ to JSON"](http://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm). – Sinatr Aug 01 '16 at 14:27
  • Not clear what your problem is. What do you mean when you say in the same JSON tag? In your example you have different property names – teo van kot Aug 01 '16 at 14:27
  • But it tries to deseralize a custom object... The thing is that my json has all the identifiers in the same json... – Sonhja Aug 01 '16 at 14:28
  • 1
    They are like 2000 identifiers. How can I read them without parsing it in a custom object? Only get the left value (identifier) and the right value? (for each line...) – Sonhja Aug 01 '16 at 14:28
  • So you just want to avoid creating a class for all the properties? Sounds like it might be better to break it down somehow, but you could use dynamic too. Like this: `dynamic d = JObject.Parse(jsonstringhere);` – rmc00 Aug 01 '16 at 14:31
  • @rmc00 You'd be still loading all the properties with `dynamic` – Matias Cicero Aug 01 '16 at 14:37
  • @MatiasCicero I thought the intent was to avoid making a class with all 2000 properties because there's too many or they might change. Maybe Sonhja can clarify... you're thinking that the intent is to not load all properties, i.e. only load some of the properties? – rmc00 Aug 01 '16 at 14:44

2 Answers2

1

The solution is like this in my case:

using (StreamReader r = new StreamReader(path))
{
     string json = r.ReadToEnd();
     JObject jsonLines = JObject.Parse(json);
     foreach (var token in jsonLines)
     {
         dtos.Add(new TokenDto { HalId = token.Key, SourceText = token.Value.ToString() });
     }
}
Sonhja
  • 8,230
  • 20
  • 73
  • 131
0

You can traverse JSON (similar to XDocument):

var json = "{\"Identifier1\":\"TextOfIdentifier1\",\"Identifier2\":\"TextOfIdentifier2\",\"Identifier3\":\"TextOfIdentifier3\"}";
foreach (var token in JObject.Parse(json).Children())
    Console.WriteLine(token.Path);

Result:

Identifier1

Identifier2

Identifier3

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319