user7597016
user7597016

Reputation:

C# Json.Net read data

Although I have figured out how I can read the JSON file with Newtonsoft, but not how I can read the points. I would like to read all X, Y points. Whats the best way to do this? I have the whole JSON file "read" and how do I get now the individual points out?

This is a small extract from the JSON file:

{
  "Points": [
    {
      "X": -3.05154,
      "Y": 4.09
    },
    {
      "X": -3.05154,
      "Y": 3.977
    }
  ],
  "Rectangles": [
    {
      "XMin": -3.08154,
      "XMax": 3.08154,
      "YMin": -4.5335,
      "YMax": 4.5335
    }
  ]
}

JObject o1 = JObject.Parse(File.ReadAllText(@"C:\Users\user\Desktop\test.json"));
Koordinaten kor = new Koordinaten();
// read JSON directly from a file
using (StreamReader file = File.OpenText(@"C:\Users\user\Desktop\test.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
    JObject o2 = (JObject)JToken.ReadFrom(reader);
}

Upvotes: 0

Views: 506

Answers (2)

Icepickle
Icepickle

Reputation: 12796

A simple way to do it would be to create a class that matches the structure of the JSON data. An example could be found here

using System;
using Newtonsoft.Json;

public class Program
{
    static string textdata = @"{
  ""Points"": [
    {
      ""X"": -3.05154,
      ""Y"": 4.09
    },
    {
      ""X"": -3.05154,
      ""Y"": 3.977
    }
  ],
  ""Rectangles"": [
    {
      ""XMin"": -3.08154,
      ""XMax"": 3.08154,
      ""YMin"": -4.5335,
      ""YMax"": 4.5335
    }
  ]
}";

    public static void Main()
    {
        var data = JsonConvert.DeserializeObject<Data>( textdata );
        Console.WriteLine( "Found {0} points", data.Points.Length );
        Console.WriteLine( "With first point being X = {0} and Y = {0}", data.Points[0].X, data.Points[0].Y);
    }
}

public class Data {
    public Point[] Points { get; set; }
}

public class Point {
    public decimal X { get; set; }
    public decimal Y { get; set; }
}

Upvotes: 1

Tatranskymedved
Tatranskymedved

Reputation: 4371

Let's split answer in 2 parts:

1. Create JSON class (model)

If You are using Visual Studio, this is easy:

  1. Create new file (class)
  2. Copy to clipboard Your JSON code above
  3. In Visual Studio go to Edit -> Paste special -> Paste JSON as Classes

This will generate You the class(es), which You can use to deserialize object:

public class Rootobject
{
    public Point[] Points { get; set; }
    public Rectangle[] Rectangles { get; set; }
}

public class Point
{
    public float X { get; set; }
    public float Y { get; set; }
}

public class Rectangle
{
    public float XMin { get; set; }
    public float XMax { get; set; }
    public float YMin { get; set; }
    public float YMax { get; set; }
}

2. Deserialize JSON into class

string allJson = File.ReadAllText(@"C:\Users\user\Desktop\test.json");
Rootobject obj = JsonConvert.DeserializeObject<Rootobject>(allJson);

Console.WriteLine($"X: {obj.Points[0].X}\tY:{obj.Points[0].Y}");

Upvotes: 1

Related Questions