Reputation: 5523
First, apologize for the long post/question. But I'm really frustrated as I can't solve this issue.
I have a JSON File that I need to convert to a readable format to store in a database .. To start, I need to read the date from the file.
Array myTextFile = File.ReadAllLines(@"C:\path\to\json\file.js");
My data in the file looks like this:
{
"javaClass": "com.untangle.app.firewall.FirewallSettings",
"rules": {
"javaClass": "java.util.LinkedList",
"list": [
{
"block": true,
"conditions": {
"javaClass": "java.util.LinkedList",
"list": [
{
"conditionType": "DST_ADDR",
"invert": false,
"javaClass": "com.untangle.app.firewall.FirewallRuleCondition",
"value": "Some String"
}
]
},
"description": "Suspecious Traffic",
"enabled": true,
"flag": true,
"javaClass": "com.untangle.app.firewall.FirewallRule",
"ruleId": 100001
},
{
"block": false,
"conditions": {
"javaClass": "java.util.LinkedList",
"list": []
},
"description": "TMP ALLOW ALL",
"enabled": false,
"flag": false,
"javaClass": "com.untangle.app.firewall.FirewallRule",
"ruleId": 100002
},
{
"block": false,
"conditions": {
"javaClass": "java.util.LinkedList",
"list": [
{
"conditionType": "DST_PORT",
"invert": false,
"javaClass": "com.untangle.app.firewall.FirewallRuleCondition",
"value": "80,443,8080"
},
{
"conditionType": "SRC_INTF",
"invert": false,
"javaClass": "com.untangle.app.firewall.FirewallRuleCondition",
"value": "non_wan"
}
]
},
"description": "Allow All HTTP(S)",
"enabled": true,
"flag": false,
"javaClass": "com.untangle.app.firewall.FirewallRule",
"ruleId": 100003
},
{
"block": true,
"conditions": {
"javaClass": "java.util.LinkedList",
"list": [
{
"conditionType": "DST_INTF",
"invert": false,
"javaClass": "com.untangle.app.firewall.FirewallRuleCondition",
"value": "wan"
},
{
"conditionType": "SRC_INTF",
"invert": false,
"javaClass": "com.untangle.app.firewall.FirewallRuleCondition",
"value": "non_wan"
}
]
},
"description": "BLOCK ALL (RULEBASE END)",
"enabled": true,
"flag": true,
"javaClass": "com.untangle.app.firewall.FirewallRule",
"ruleId": 10004
}
]
},
"version": 1
}
After that, I converted my JSON using https://quicktype.io/ to a C# Class which gave:
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var data = GettingStarted.FromJson(jsonString);
//
namespace QuickType
{
using System;
using System.Net;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class GettingStarted
{
[JsonProperty("rules")]
public Rules Rules { get; set; }
[JsonProperty("javaClass")]
public string JavaClass { get; set; }
[JsonProperty("version")]
public long Version { get; set; }
}
public partial class Rules
{
[JsonProperty("javaClass")]
public string JavaClass { get; set; }
[JsonProperty("list")]
public List[] List { get; set; }
}
public partial class List
{
[JsonProperty("enabled")]
public bool Enabled { get; set; }
[JsonProperty("conditions")]
public Conditions Conditions { get; set; }
[JsonProperty("block")]
public bool Block { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("javaClass")]
public string JavaClass { get; set; }
[JsonProperty("flag")]
public bool Flag { get; set; }
[JsonProperty("ruleId")]
public long RuleId { get; set; }
}
public partial class Conditions
{
[JsonProperty("javaClass")]
public string JavaClass { get; set; }
[JsonProperty("list")]
public OtherList[] List { get; set; }
}
public partial class OtherList
{
[JsonProperty("invert")]
public bool Invert { get; set; }
[JsonProperty("conditionType")]
public string ConditionType { get; set; }
[JsonProperty("javaClass")]
public string JavaClass { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public partial class GettingStarted
{
public static GettingStarted FromJson(string json) => JsonConvert.DeserializeObject<GettingStarted>(json, Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this GettingStarted self) => JsonConvert.SerializeObject(self, Converter.Settings);
}
public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}
}
From here, I'm stuck on how to deal with the data I read in myTextFile
.. Can't figure out how to get the data in tabular structure or any other readable format which I can perform other operations on later on.
Upvotes: 0
Views: 2706
Reputation: 1579
As you have specified in some comments you are using JSON.Net, and you have a class that resembles the structure of your JSON file. Your generated code from QuickType has given you (what looks to be) everything you need. where you have access to a single string for your whole file, You should call the following:
var myString = File.ReadAllText(@"C:\path\to\json\file.js");
var myObject = GettingStarted.FromJson(myString);
myObject
will then be an instance of the class generated by QuickType. you can then do whatever you like with that object (include passing it into your data access code for database storage)
This doesn't look like much of a problem to me. QuickType looks like it's generated everything you need for use with Json.Net
Upvotes: 1
Reputation: 1386
No need to apologize about long post. It's always good to post your code :)
I think you need to look at as a list of higher level tasks instead of diving into your implementation.
Yours steps should be as follows:
Also keep in mind you will need to choose a database and setup/configure that. There are many around that you could use to develop on your local machine. I also haven't used this personally, but SQL Expresses seems to be a good choice for C#. Some googling should help you figure out how to do this.
Upvotes: 1