-2

I am faced with a problem, I want to obtain the value from a JSON property but the key is unknown when the request is made. I know I can stringify the JSON response and splice based on the ":" and remove the other extraneous characters, but I want to know if there is a simple way to just return the value of my JSON response object.

Here is an example:

JSON { 123456789: "DATA I NEED"}

123456789 is a unique ID that will vary based on the query string entry.

Johnny
  • 3
  • 2

2 Answers2

1

try this

var json= "{ 123456789: \"DATA I NEED\"}";

var jsonParsed=JObject.Parse(json);

var value= jsonParsed.Properties().ToArray()[0].Value.ToString(); //DATA I NEED

//or
var val = (string) JObject.Parse(json).Values().ToArray()[0];

//or using LINQ
var val = (string) JObject.Parse(json).Values().FirstOrDefault();
Serge
  • 40,935
  • 4
  • 18
  • 45
0

Once you've deserialized the JSON you can simply return a list of keys.

using System.Linq;

List<String> myKeys = myDict.Keys.ToList();