Okay, so first off the JSON you supplied had a few errors to it. Assuming this was from just creating the question and there's not a larger issue, the final result should look similar to this:
[{\"bandanaColor\":\"rgb(0,0,255)\",\"ninja\":\"Leonardo - $0.99\",\"data\":[[1336485655241,0.99],[1336566333236,0.99],[1336679536073,0.99],[1336706394834,0.99],[1336774593068,0.99],[1366284992043,0.99]]},{\"bandanaColor\":\"rgb(128,0,128)\",\"ninja\":\"Donatello - $0.25\",\"data\":[[1361061084420,0.23],[1366102471587,0.25],[1366226367262,0.25],[1366284992043,0.25]]},{\"bandanaColor\":\"rgb(255,0,0)\",\"ninja\":\"Raphael - $0.15\",\"data\":[[1327305600000,0.15],[1365583220422,0.15],[1365669396241,0.15],[1365669396241,0.15],[1365753433493,0.15],[1366284992043,0.15]]},{\"bandanaColor\":\"rgb(255,165,0)\",\"ninja\":\"Michelangelo - $0.14\",\"data\":[1366284992043,0.14]}]
(Which can be tested using a site like this one.)
Now, as far as parsing, Regex is going to be problematic. there are a lot of different obstacles to JSON, so a parser is going to be the best bet. And, to avoid re-inventing the wheel, the Json.NET
library happens to work amazingly welll. To give an example:
/* includes
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
*/
// the original JSON string
String jsonString = "[{\"bandanaColor\":\"rgb(0,0,255)\",\"ninja\":\"Leonardo - $0.99\",\"data\":[[1336485655241,0.99],[1336566333236,0.99],[1336679536073,0.99],[1336706394834,0.99],[1336774593068,0.99],[1366284992043,0.99]]},{\"bandanaColor\":\"rgb(128,0,128)\",\"ninja\":\"Donatello - $0.25\",\"data\":[[1361061084420,0.23],[1366102471587,0.25],[1366226367262,0.25],[1366284992043,0.25]]},{\"bandanaColor\":\"rgb(255,0,0)\",\"ninja\":\"Raphael - $0.15\",\"data\":[[1327305600000,0.15],[1365583220422,0.15],[1365669396241,0.15],[1365669396241,0.15],[1365753433493,0.15],[1366284992043,0.15]]},{\"bandanaColor\":\"rgb(255,165,0)\",\"ninja\":\"Michelangelo - $0.14\",\"data\":[1366284992043,0.14]}]";
// The parsed result (using Json.NET)
var json = JsonConvert.DeserializeObject(jsonString);
// Grab all the "ninjas"
String[] ninjas = (json as JArray).Select (x => x.Value<String>("ninja")).ToArray();
// Begin aggregating the results
IDictionary<String, Double> result = ninjas.ToDictionary(
x => x.Substring(0, x.IndexOf(" - ")),
y => {
Double d;
return Double.TryParse(y.Substring(y.IndexOf(" - ") + 4), out d) ? d : default(Double);
}
);
Gives you:
Key Value
Leonardo 0.99
Donatello 0.25
Raphael 0.15
Michelangelo 0.14
I used simple string parsing (splitting by the instance of -
) but you can get more elaborate if necessary.
Second Version
A little more investment time (since you're not creating an object) but the payoff is that you're not having to learn the Json syntax for dealing with JObject, JArray, etc. Instead, it serializes the data in to classes (you've created) making retrieving the information a bit more fluent. e.g.
public class ParentObj
{
public String bandanaColor;
public String ninja;
public String NinjaName
{
get
{
String ninja = this.ninja ?? String.Empty;
Int32 i = ninja.IndexOf(" - ");
return i != -1 ? ninja.Substring(0, i) : String.Empty;
}
}
public Double NinjaPrice
{
get
{
String ninja = this.ninja ?? String.Empty;
Double price;
Int32 i = ninja.IndexOf(" - $");
return i != -1 && Double.TryParse(ninja.Substring(i + 4), out price) ? price : default(Double);
}
}
}
void Main()
{
// the original JSON string
String jsonString = "[{\"bandanaColor\":\"rgb(0,0,255)\",\"ninja\":\"Leonardo - $0.99\",\"data\":[[1336485655241,0.99],[1336566333236,0.99],[1336679536073,0.99],[1336706394834,0.99],[1336774593068,0.99],[1366284992043,0.99]]},{\"bandanaColor\":\"rgb(128,0,128)\",\"ninja\":\"Donatello - $0.25\",\"data\":[[1361061084420,0.23],[1366102471587,0.25],[1366226367262,0.25],[1366284992043,0.25]]},{\"bandanaColor\":\"rgb(255,0,0)\",\"ninja\":\"Raphael - $0.15\",\"data\":[[1327305600000,0.15],[1365583220422,0.15],[1365669396241,0.15],[1365669396241,0.15],[1365753433493,0.15],[1366284992043,0.15]]},{\"bandanaColor\":\"rgb(255,165,0)\",\"ninja\":\"Michelangelo - $0.14\",\"data\":[1366284992043,0.14]}];";
// The parsed result (using Json.NET in to our custom object)
IEnumerable<ParentObj> json = JsonConvert.DeserializeObject<IEnumerable<ParentObj>>(jsonString.TrimEnd(';'));
// the use Linq to create a dictionary from our custom getters
IDictionary<String, Double> result = json.ToDictionary (x => x.NinjaName, y => y.NinjaPrice);
}
Same outcome as above, but now the parsing is done through the ParenObj
class with custom getters.