首先,为这个冗长的帖子/问题道歉。但是我真的很沮丧,因为我不能解决这个问题。
我有一个JSON文件,我需要将其转换为可读格式以存储在数据库中。首先,我需要从文件中读取日期。
Array myTextFile = File.ReadAllLines(@"C:\path\to\json\file.js");我在文件中的数据如下所示:
{
"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
}在那之后,我使用https://quicktype.io/将我的JSON转换为C#类,它提供了:
// 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,
};
}
}从这里开始,我被困在如何处理我在myTextFile中读取的数据上。我不知道如何以表格结构或任何其他可读格式获取数据,以便稍后对其执行其他操作。
发布于 2017-10-12 23:19:13
没有必要为过长的帖子道歉。发布你的代码总是很好的:)
我认为你需要把它看作一个更高层次的任务列表,而不是一头扎进你的实现。
您的步骤应该如下:
还要记住,您将需要选择一个数据库并对其进行设置/配置。您可以使用周围的许多工具在本地计算机上进行开发。我也没有亲自使用过,但是SQL Expresses似乎是C#的一个很好的选择。用谷歌搜索一下应该能帮你弄清楚怎么做。
发布于 2017-10-13 17:41:17
正如您在一些注释中指定的那样,您正在使用JSON.Net,并且您有一个类似于JSON文件结构的类。您从QuickType生成的代码已经为您提供了(看起来像是)所需的一切。如果您有权访问整个文件的单个字符串,则应调用以下代码:
var myString = File.ReadAllText(@"C:\path\to\json\file.js");
var myObject = GettingStarted.FromJson(myString);然后,myObject将成为由QuickType生成的类的实例。然后,您可以随心所欲地处理该对象(包括将其传递到用于数据库存储的数据访问代码中)。
对我来说,这看起来不是什么大问题。QuickType看起来已经生成了使用Json.Net所需的所有内容
https://stackoverflow.com/questions/46713039
复制相似问题