Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one json file which as content as follows :

[ { "type": "line", "a": "-1,5; 3,4", "b": "2,2; 5,7", "color": "127; 255; 255; 255" }, { "type": "circle", "center": "0; 0", "radius": 15.0, "filled": false, "color": "127; 255; 0; 0" }, { "type": "triangle", "a": "-15; -20", "b": "15; -20,3", "c": "0; 21", "filled": true, "color": "127; 255; 0; 255" } ]

So I want to read this file using C# on a windows form and display it . How can I do that? additionally if possible I need flexible option to select file other than json also.

What I have tried:

I am still searching for solution
Posted
Updated 5-Sep-21 4:48am

1 solution

Start by running the JSON through a "JSON to C#" converter - it will construct classes based on the data. Here's one I use: Convert JSON to C# Classes Online - Json2CSharp Toolkit[^]
And here's what it generates:
C#
public class Root
{
    public string type { get; set; }
    public string a { get; set; }
    public string b { get; set; }
    public string color { get; set; }
    public string center { get; set; }
    public double? radius { get; set; }
    public bool? filled { get; set; }
    public string c { get; set; }
}

Then all you need is to deserialize the data into your class instance - I use NewtonSoft: Json.NET - Newtonsoft[^] which is avialable in VS via NuGet:
C#
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
 
Share this answer
 
Comments
Digvijay Gaikwad 5-Sep-21 10:56am    
Thanks for providing solution @OriginalGriff but I dont want to convert json I want to display it without converting
OriginalGriff 5-Sep-21 10:59am    
Then you need to be a lot more precise in asking questions :laugh:

If it's just a text file, what's the problem? There are loads of ways to display text, and none of them are complicated ...
Digvijay Gaikwad 5-Sep-21 15:02pm    
The task for the challenge is to to implement a simple vector graphic viewer:
Main Task
1. The vector graphic viewer should read data from JSON file and display the result on the screen.
2. The viewer can show primitives of the following types:
a. Line. A line is described by coordinates of edges and color.
b. Circle. A circle is described by coordinates of center, radius, color and fill flag (if the
circle is filled or not).
c. Triangle. A triangle is described by coordinates of edges, color and fill flag.
3. Data types:
a. Color is represented as ARGB value.
b. Coordinate is represented as a pair of floats X and Y. The coordinate system is Cartesian:
4. As X and Y can be any valid float numbers and window size is limited to a screen resolution, all
primitives should be automatically and proportionally scaled to be fit for the window.

Desing note
The solution should be designed in the way, that the following requirements could be easly
implemented in the future:
1. To design and implement rectangles processing.
The fourth type of primitives, the rectangle, should be introduced to the application.
2. To implement other data source reading.
In addition to the JSON input file format, the application should be able to read a primitive list
from other data source (e.g. XML files).
3. To select primitives with mouse clicks and show properties.
The user should be able to click onto the primitive to see its properties (points, color, line type).
Simple example of input file
There is a simple example of input file representing a list of three primitives (line, circle and triangle):
[
{
"type": "line",
"a": "-1,5; 3,4",
"b": "2,2; 5,7",
"color": "127; 255; 255; 255"
},
{
"type": "circle",
"center": "0; 0",
"radius": 15.0,
"filled": false,
"color": "127; 255; 0; 0"
},
{
"type": "triangle",
"a": "-15; -20",
"b": "15; -20,3",
"c": "0; 21",
"filled": true,
"color": "127; 255; 0; 255"
}
]

This is what I want to do.
OriginalGriff 5-Sep-21 15:46pm    
So what have you done so far?
Digvijay Gaikwad 5-Sep-21 16:17pm    
I just started learning WPF so not able to code for this . Your help is appreciated

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