在下面的情况下,如何有效地利用C#中的子字符串 [英] How do I make use of substring in C# effectively in the below situation
问题描述
我有一个文本文件EventInput.txt,内容为:
---------------------------- ----------------------------
i have a text file EventInput.txt with content:
--------------------------------------------------------
{
"@odata.type" : "#Event.1.0.0.Event",
"Events" :
[
{
"EventID" : "9ea74dad-9e33-5549-b2ea-a80217d82315",
"EventTimestamp" : "2017-09-07T10:36:47Z",
"EventType" : "Alert",
"MessageArgs" : [ "Port 1I Box 1 Bay 5", "Slot 0" ],
"MessageID" : "iLOEvents.0.9.DrvArrPhysDrvErasing",
"OriginOfCondition" :
"/rest/v1/Systems/1/SmartStorage/ArrayControllers/2/DiskDrives/20",
"Severity" : "Warning"
}
],
"Name" : "Events",
"Type" : "Event.1.0.0"
}
----------------- ----------------------------------
我需要以下信息在c#:
EventID = 9ea74dad-9e33-5549-b2ea-a80217d82315
EventTimestamp = 2017-09-07T10:36:47Z
EventType =警报
MessageArgs = Port1I,Box1,Bay 5,Slot 0
MessageID = iLOEvents.0.9.DrvArrPhysDrvErasing
OriginOfCondition = / rest / v1 / Systems / 1 / SmartStorage / ArrayControllers / 2 / DiskDrives / 20
严重性=警告
我尝试了什么:
i已经尝试了以下代码来获取EventTimestamp。您能否提出更好的解决方法:
---------------------------------------------------
I need the following information out of it in c#:
EventID=9ea74dad-9e33-5549-b2ea-a80217d82315
EventTimestamp=2017-09-07T10:36:47Z
EventType=Alert
MessageArgs=Port1I,Box1,Bay 5,Slot 0
MessageID=iLOEvents.0.9.DrvArrPhysDrvErasing
OriginOfCondition=/rest/v1/Systems/1/SmartStorage/ArrayControllers/2/DiskDrives/20
Severity=Warning
What I have tried:
i have tried out the following code for fetching EventTimestamp. Could you please suggest better ways to solve this:
class Program
{
static void Main(string[] args)
{
string stringEventReceived =System.IO.File.ReadAllText(@"C:\EventInput.txt");
Console.WriteLine("EVENT LOG FILE text:" + stringEventReceived);
string stringToFind = "EventTimestamp";
bool stringMatchResult = stringEventReceived.Contains(stringToFind);
if (stringMatchResult)
{
int index = stringEventReceived.IndexOf(stringToFind);
if (index >= 0)
Console.WriteLine("{0} begins at character position {1}", stringToFind, index + 1);
}
string RESULT = stringEventReceived.Substring(143, 40);
Console.WriteLine("RESULT TIMESTAMP: \n" + RESULT);
Console.ReadKey();
}
}
推荐答案
您不需要使用子字符串,有一个更简单的解决方案。本文将向您展示如何使用JSON数据:在C#和amp中使用JSON ; VB [ ^ ]
You don't need to use substring, there is a far simpler solution. This article will show you how to work with JSON data: Working with JSON in C# & VB[^]
试试这个
try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
public class RootObject
{
public string odatatype { get; set; }
public List<Event> Events { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
public class Event
{
public string EventID { get; set; }
public string EventTimestamp { get; set; }
public string EventType { get; set; }
public List<string> MessageArgs { get; set; }
public string MessageID { get; set; }
public string OriginOfCondition { get; set; }
public string Severity { get; set; }
}
static void Main(string[] args)
{
string path = @"D:\Projects\TextFile1.txt";
string stringEventReceived = System.IO.File.ReadAllText(path);
string temp = stringEventReceived.Replace("@odata.type", "odatatype");
JavaScriptSerializer js = new JavaScriptSerializer();
var obj = js.Deserialize<RootObject>(temp);
string EventID = obj.Events[0].EventID;
string EventTimestamp = obj.Events[0].EventTimestamp;
string MessageID = obj.Events[0].MessageID;
string MessageArgs = string.Join(",", obj.Events[0].MessageArgs);
string OriginOfCondition = obj.Events[0].OriginOfCondition;
}
}
}
参考文章in解决方案1
refer the article in solution 1
这篇关于在下面的情况下,如何有效地利用C#中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!