-2

I have a problem; I would to know if there is a method to parse json file without having a unique format. So it may have different attributes but all of them contain the attribute Status but it can be in double.

  {
  "requestid": "1111",
  "message": "db",
  "status": "OK",
  "data": [
    {
      "Status": "OK", // this one I would to test first to read the other attributes
      "fand": "",
      "nalDate": "",
      "price": 1230000,
      "status": 2
    }
  ]
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Adouani Riadh
  • 1,162
  • 2
  • 14
  • 37

2 Answers2

1

The defacto standard Json serializer for .NET is Newtonsoft.Json (How to install). You can parse the Json into an object graph and work on that in any order you like:

namespace ConsoleApp3
{
    using System;
    using Newtonsoft.Json.Linq;

    class Program
    {
        static void Main()
        {
            var text = @"{
                'requestid': '1111',
                'message': 'db',
                'status': 'OK',
                'data': [
                {
                    'Status': 'OK', // this one I would to test first to read the other attributes
                    'fand': '',
                    'nalDate': '',
                    'price': 1230000,
                    'status': 2
                }
                ]
            }";

            var json = JObject.Parse(text);

            Console.WriteLine(json.SelectToken("data[0].Status").Value<string>());
            Console.ReadLine();
        }
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

With https://www.newtonsoft.com/json

Data data = JsonConvert.DeserializeObject<Data>(json);

And create the class Data with the interesting data inside the json

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32