Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to parse the keyValue Pair from the file without using the Class .

Basically what i want is :
Deserialize Json from file in C#


What I have tried:

MyOutput.json

    {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111112",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111112",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "14.0000",
          "Value": 109.0909
        }
      ]
    }
  },
  {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111113",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111113",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "12.0000",
          "Value": 109.0909
        }
      ]
    }
  }



<programs.cs>
static void Main(string[] args)
       {


           string jsonFilePath = @"Output.json";

           string json = File.ReadAllText(jsonFilePath);

           var dict = JsonConvert.DeserializeObject<Dictionary<string, Object>>(json);
           foreach (var kv in dict)
           {
               Console.WriteLine(kv.Key + ":" + kv.Value);
           }


       }



I want to print Data like :(One case1)
Platform Parent Dato Id: 23768
Case2:
and I am using a NewtonJson in order to get the Value Rate from the Value Information nest.

Output Should be
Value Rate": 14.0000

Thanks for the advanced
Looking forward for your response
Posted
Updated 13-Jul-22 0:01am

I wrote an article based on questions like yours in here that has the answers that you want: Working with JSON in C# & VB[^]
 
Share this answer
 
The Newtonsoft JSON package is designed to work with hierarchical classes: it has no provision for "random access" to properties.
DeserializeObject returns a class instance which contains the full "tree" of contained objects in the JSON data - it doesn't return key/value pairs!

Use a JSON-to-C#-Class converter like this: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^] and it will construct the class templates you need to access the data:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    public class PlatformCompensation
    {
        [JsonProperty("Platform mission Id")]
        public string PlatformMissionId { get; set; }

        [JsonProperty("Platform submission Id")]
        public string PlatformSubmissionId { get; set; }

        [JsonProperty("Platform submission Flight Id")]
        public string PlatformSubmissionFlightId { get; set; }

        [JsonProperty("Value Rate")]
        public string ValueRate { get; set; }
        public double Value { get; set; }
    }

    public class Root
    {
        [JsonProperty("Platform Parent Dato Id")]
        public string PlatformParentDatoId { get; set; }

        [JsonProperty("Platform Dato Id")]
        public string PlatformDatoId { get; set; }

        [JsonProperty("Platform Dato Name")]
        public string PlatformDatoName { get; set; }

        [JsonProperty("Platform mission Id")]
        public string PlatformMissionId { get; set; }

        [JsonProperty("Platform submission Id")]
        public string PlatformSubmissionId { get; set; }

        [JsonProperty("Platform submission Flight Id")]
        public string PlatformSubmissionFlightId { get; set; }

        [JsonProperty("Start Date")]
        public string StartDate { get; set; }

        [JsonProperty("End Date")]
        public string EndDate { get; set; }

        [JsonProperty("Platform Compensation")]
        public double PlatformCompensation { get; set; }

        [JsonProperty("Total Value")]
        public double TotalValue { get; set; }
        public string Goal { get; set; }

        [JsonProperty("Value Information")]
        public ValueInformation ValueInformation { get; set; }
    }

    public class ValueInformation
    {
        [JsonProperty("Platform Compensation")]
        public List<PlatformCompensation> PlatformCompensation { get; set; }
    }

Then it's pretty trivial to get the data you want in a way that is easy to read, easy to understand, and easy to maintain.
 
Share this answer
 
Comments
Member 14837073 13-Jul-22 8:00am    
var jsonFilePath = @"C:\Test2\JosonParser - Copy\JosonParser\json2.json";

var jsonStr = File.ReadAllText(jsonFilePath);

JObject obj = JObject.Parse(jsonStr);

//Select for Particalar tokens
string value_name =obj.SelectToken("['Value Information']['Platform Compensation'][0].Value").ToString();
Console.WriteLine(value_name);



Its has resolved.
but if there any problem while if i used SelectToken
If you don't want to use specific classes, then you'll have to use LINQ to JSON, with the JObject class:

LINQ to JSON[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900