Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 10, 2018 2:15 PM
Hi,
I've got a bunch of .json files and I'd like to display them in a C# (Win Form) program. I've added JSON.NET to my program and I made a button to let the user choose which .json file to open. The program compiles, but when I run it in debug mode and I try to open a .json file, it crashes.
This is the .json file I'm trying to display: https://www.opfitalia.net/Server/test.json
This is the code I came up to (although it doesn't work):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Newtonsoft.Json;
using System.IO;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Select the JSON file you want to open");
OpenFileDialog myfileDlg = new OpenFileDialog();
DialogResult result = myfileDlg.ShowDialog();
if (myfileDlg.ShowDialog() == DialogResult.OK)
{
string path = myfileDlg.FileName;
MessageBox.Show(path);
//var jsonText = File.ReadAllText(path);
using (StreamReader r = new StreamReader(path))
{
string json = r.ReadToEnd();
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
}
/*
dynamic array = JsonConvert.DeserializeObject(jsonText);
foreach(var item in array)
{
MessageBox.Show("{0} {1}", item.temp, item.vcc);
}
*/
/*
using (StreamReader file = System.IO.File.OpenText(path))
{
var jArray = Newtonsoft.Json.Linq.JArray.Parse(file.ReadToEnd());
return Json(jArray);
}
*/
}
}
The debugger reports "Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: M. Path '', line 0, position 0.'"
Any guess?
EDIT: I also tried with the following code, but it displays the same error:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Select the JSON file you want to open");
OpenFileDialog myfileDlg = new OpenFileDialog();
DialogResult result = myfileDlg.ShowDialog();
if (myfileDlg.ShowDialog() == DialogResult.OK)
{
string path = myfileDlg.FileName;
MessageBox.Show(path);
using (StreamReader file = File.OpenText(path))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
}
EDIT: Oh, I got it, it's because the first lines of the JSON file are considered invalid. It's the very first time that I deal with .json files.
Now, second step, how can I display it in my C# program? I'd like something like... I don't know... something that looks nice and human-readable so the user can read it... Something like populate a listview using the .json file.
I was thinking about something like:
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
//second block
var items = o2["results"]
.Children<JObject>()
.Select(jo => new ListViewItem(new string[]
{
(string)jo["shots"],
(string)jo["faces-vip-people"],
(string)jo["object"],
(string)jo["speech-to-text"],
(string)jo["compliance-explicit"],
(string)jo["compliance-sw"],
(string)jo["settings"],
(string)jo["top-moments-sport"],
(string)jo["sentiment"],
(string)jo["topics"],
(string)jo["brands"]
}
))
.ToArray();
FlarmListView.View = View.Details;
FlarmListView.FullRowSelect = true;
FlarmListView.Columns.Add("shots");
FlarmListView.Columns.Add("faces-vip-people");
FlarmListView.Columns.Add("object");
FlarmListView.Columns.Add("speech-to-text");
FlarmListView.Columns.Add("compliance-explicit");
FlarmListView.Columns.Add("compliance-sw");
FlarmListView.Columns.Add("settings");
FlarmListView.Columns.Add("top-moments-sport");
FlarmListView.Columns.Add("sentiment");
FlarmListView.Columns.Add("topics");
FlarmListView.Columns.Add("brands");
FlarmListView.Items.AddRange(items);
}
All replies (3)
Sunday, June 10, 2018 6:13 PM âś…Answered
Got it.
I'm gonna post a new question. By the way, I need a generalised utility that can dynamically show any format, 'cause it won't always be the same. As to the UI, I needed to show the .json objects in a listview. Thank you anyway.
For all the other people that are gonna see this thread in the future, the way I solved it was to use JSON.NET like so:
using (StreamReader file = File.OpenText(path))
using (JsonTextReader reader = new JsonTextReader(file))
{
JObject o2 = (JObject)JToken.ReadFrom(reader);
}
"path" is the path to the .json file you wanna open. After that, it is possible to deal with .json objects in C# and do whatever you want, like use objects or display them. If the .json file is not conform to the syntax or is invalid and so on, the parsing just stops and shows an error, so I think it's better to add a try-catch step, just to avoid to have the program to crash if something goes wrong.
That's about it.
Sunday, June 10, 2018 5:08 PM
You solved the problem already so you should close this question and start a new one. Close it by replying to the original thread with the answer (instead of updating the question) then mark that reply as the answer.
This is not a Visual Studio question. You should ask your new question in an appropriate forum. It is not clear what you are asking (especially since the new question hijacked the original question) but if you are asking how to create a UI then that probably would be a Windows Forms question or a WPF question.
Something else that is not clear is if the format of the JSON data will always be the same or if you need a generalized utility that can dynamically show any format. When I say format, I mean the objects in the data. If you don't understand what I mean when I say that JSON data normally has specific objects then you probably need to learn more about JSON.
Sam Hobbs
SimpleSamples.Info
Sunday, June 10, 2018 6:35 PM
A Listview probably will not work. JSON data is hierarchical. Some JSON data could be shown using a Listview but for a generalized viewer you will need something else, such as a tree view.
Sam Hobbs
SimpleSamples.Info