Parse JSON style text with filehelpers

563 views Asked by At

So in a CSV file we have this "foo='foo value';bar='bar value';" as one column value

I have done some research and figure worst case I can simply enclose that value with { "..." } and replace ';' with ',' and '=' with ':' then just use a JSON parse but i am new to file helpers so i figure there could easily be a way to accomplish this with that lib no extra work. I have been searching around but likely not asking google the right question.

that said i am posting the question.

worst case i am going to build out a custom parser and do the above.

hope this question is clear, if not please ask and i will edit best i can to clarify.

1

There are 1 answers

0
Kraang Prime On

This method should handle reading in the values from the format you supplied and return a Dictionary containing all the values in keyPairs.

public Dictionary GetData(string data) {
    Dictionary<string, string> dictRet = new Dictionary<string, string>();

    strKeyPairs = data.Split(new[] { ";" }, StringSplitOptions.None);

    foreach (string strKeyPair in strKeyPairs) {
        strKeyValuePair = strKeyPair.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
        if (strKeyValuePair.Length > 0) {
            if (strKeyValuePair.Length == 2) {
                dictRet[strKeyValuePair[0]] = strKeyValuePair[1];
            } else {
                dictRet[strKeyValuePair[0]] = string.Empty;
            }
        }

    }

    return dictRet;
}