I have data that looks like this.... how can i parse this in C# .NET
[
[
WALLET_TYPE,
CURRENCY,
BALANCE,
UNSETTLED_INTEREST,
AVAILABLE_BALANCE,
LAST_CHANGE,
TRADE_DETAILS
],
[
WALLET_TYPE,
CURRENCY,
BALANCE,
UNSETTLED_INTEREST,
AVAILABLE_BALANCE,
LAST_CHANGE,
TRADE_DETAILS
]
]
Please note the inside can have N records..... i just showed 2. If this were Json, i would be ok, but this aint json, how can I do it the correct way in C# that is neat and clean.....
This came as a string, not an array, etc..... so how do i parse this string?
I have TRIED Json parsing, CSV parsing, config file parsing, profile parsing, and INI file parsing, this seem not at all it. I am NEW to programming. Please help
CodePudding user response:
Using some regex we can make the data into valid JSON, ASSUMING that the data is as provided.
const string data = "[[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS],[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS]]";
//Using Regex we can wrap all UPPERCASE_WORDS with "'s making it valid JSON
var jsonData = Regex.Replace(data, "([A-Z_] )", "\"$&\"");
var parsedData = JsonConvert.DeserializeObject<List<List<string>>>(jsonData);
Now you have a valid List of List<string>.
Here's the same data / regex in regex101 https://regex101.com/r/xoc7MK/1
Edit Assuming there is always 7 items in each array:
const string data = "[[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS],[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS]]";
//Remove all but Letters, Numbers, Commas, Underscores and punctuations
var dataCsv = Regex.Replace(data, "[^a-zA-Z0-9_.,]", "");
var items = dataCsv.Split(',');
List<List<string>> itemsList = new List<List<string>>();
for(int i = 0; i < items.Length; i = 7) {
itemsList.Add(new List<string>(items.Skip(i).Take(7)));
}
CodePudding user response:
To parse the data you've shown, you can use a library like Json.NET. Here's an example of how you might do this:
Install the Json.NET NuGet package by running the following command in the Package Manager Console:
Install-Package Newtonsoft.Json
Add the following using statement to the top of your file:
using Newtonsoft.Json;
Parse the data using the
JsonConvert.DeserializeObject
method:string data = "[[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS],[WALLET_TYPE,CURRENCY,BALANCE,UNSETTLED_INTEREST,AVAILABLE_BALANCE,LAST_CHANGE,TRADE_DETAILS]]"; var parsedData = JsonConvert.DeserializeObject<List<List<string>>>(data);
This will convert the string data into a List<List<string>>
, which is a list of lists of strings. Each inner list represents a record, and each string in the inner list represents a field in that record. You can access the data like this:
// Get the first record
var firstRecord = parsedData[0];
// Get the WALLET_TYPE field of the first record
var walletType = firstRecord[0];