-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
  • 104,344
  • 11
  • 159
  • 196
Adouani Riadh
  • 1,060
  • 1
  • 12
  • 34

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
  • 68,786
  • 25
  • 88
  • 134
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,000
  • 2
  • 10
  • 29