-3

I have a string that will be like this:

[
{
    "info": "thing 1 by person 1",
    "glow": "0xff9900",
    "glowalpha": 1,
    "glowsize": 2,
    "glow2": "0xabcdef",
    "glowalpha2": 1,
    "glowsize2": .4,
    "glow3": "0x1e1e1e",
    "glowalpha3": .5,
    "glowsize3": .1,
    "paletteSwap"{
        "colors":[0,-500]
        "replacements":[-26368,99]
    },
    "paletteSwapPA"{
        "colors":[0,-500]
        "replacements":[-26368,99]
    }
},
{
    "info": "another thing by person 2",
    "glow": "0xff9900",
    "glowalpha": 1,
    "glowsize": 2,
    "paletteSwap"{
        "colors":[0,99,76,832638]
        "replacements":[-26368,847261,9387,92812]
    },
    "paletteSwapPA"{
        "colors":[0,99,76,832638]
        "replacements":[-26368,847261,9387,92812]
    }
},
//and so on. The last "phrase" won't have a comma at the end.
]

I can get rid of the beginning and ending [ ] easily—that's easily handled by Substrings. How do I manage to separate those "phrases" into an ArrayList, such as

thing[0] =

{
    "info": "thing 1 by person 1",
    "glow": "0xff9900",
    "glowalpha": 1,
    "glowsize": 2,
    "glow2": "0xabcdef",
    "glowalpha2": 1,
    "glowsize2": .4,
    "glow3": "0x1e1e1e",
    "glowalpha3": .5,
    "glowsize3": .1,
    "paletteSwap"{
        "colors":[0,-500]
        "replacements":[-26368,99]
    },
    "paletteSwapPA"{
        "colors":[0,-500]
        "replacements":[-26368,99]
    }
}

, thing[1] =

{
    "info": "another thing by person 2",
    "glow": "0xff9900",
    "glowalpha": 1,
    "glowsize": 2,
    "paletteSwap"{
        "colors":[0,99,76,832638]
        "replacements":[-26368,847261,9387,92812]
    },
    "paletteSwapPA"{
        "colors":[0,99,53315]
        "replacements":[-26368,847261,67543]
    }
}

and so on? I simply can't use .split(',')—that would also separate the things inside the "phrases", and I wouldn't easily be able to glue them back together, because there's no consistent line count for them.

What do I do?

E_net4
  • 27,810
  • 13
  • 101
  • 139

2 Answers2

0

As the comments suggest, it seems you are trying to parse JSON in C# and the Newtonsoft library is very good for this. However, if for some reason you do not want to use a library, I believe the correct way to do this is using the equivalent of a stack:

You can easily find the first { as it will be the first element in your string. Now the question is how to find its associated }. Well, the issue will be that there can be many { and } before the } associated with the first curly brace appears. However, notice that as you loop through the string, you can never have a case where you will have seen more } than {, by the nature of the JSON tree structure. In fact, as soon as you have seen an equivalent number of { and }, you have found a pair of brace. So do the following (in pseudo code):

 int counter = 0;
 int openBraceIdx = position;
 int closedBraceIdx;

 foreach elem in your string starting from openBraceIdx:
     if elem == "{":
         counter++
     if elem == "}":
        counter--
     if counter == 0:
        closedBraceIdx = elemIdx;
        break;

(obviously checking not to exceed the bounds of the string and what not)

This will get you the associated } position. You can then call the above code again to You can now splice and cut the string as you desire. If you are trying to parse the JSON string, you should create a recursive function to do this, and you will need to create a JSON class object to store your results. Again, if this is the case, your life will be much easier using a library like Newtonsoft.

fishrec
  • 70
  • 8
0

As mentioned, there are tools that may do the job for you, but in case you want to go the hard way, you may try to use a brace counter algorithm, provided that any opening brace has a closing brace counterpart. With your string format it would be something like

    public List<string> GetMeaningfullParts(string AllInfo)
    {
        List<string> PartsList = new List<string>();

        int braceLevel = 0;
        int startIndex = 0;
        for(int i=1; i<AllInfo.Length; i++) //first '[' avoided
        {
            char nextChar = AllInfo[i];
                if (nextChar == '{')
                {
                    braceLevel++;
                    if (braceLevel == 1)
                        startIndex = i;
                }
                else if (nextChar == '}')
                {
                    braceLevel--;
                    if (braceLevel == 0)
                    {
                        PartsList.Add(AllInfo.Substring(startIndex, i - startIndex + 1));
                    }
                }
                
        }

        return PartsList;
    }

Disclaimer: Performance may not be the best.

Amo Robb
  • 810
  • 5
  • 11