JSON string to List

I’m grabbing a json string from a mysql database with php. What I want to do is to be able to save that json as a CSV file. I know how to make that file with a list, but I’m not sure how to do it with a json string.
I know it has to be parsed first, the I would imagine add it to the list, but I’m missing something.
public void SaveOilToCSV(string CSVList)
Debug.Log (CSVList);

		var oilchangelist = JSON.Parse(CSVList);
		foreach(JSONNode oc in oilchangelist)
			oilchangelist.Add (oc);

It spits out an error: InvalidOperationException: Collection was modified; enumeration operation may not execute.

Just for jollys, here is the rest of the code to give you a frame of reference:

List<string>OilChangeList =new List<string>() ;
				StringBuilder csvcontent = new StringBuilder();
				csvcontent.AppendLine("Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price");
				for(int i=0; i <OilChangeList.Count; i++ )
					csvcontent.AppendLine (OilChangeList<em>.ServiceDate+", "+OilChangeList_.Location+", "+OilChangeList*.Mileage+"," +*_</em>

" “+OilChangeList.Labor+”, “+OilChangeList_.OilBrand+”, “+ OilChangeList*.OilPrice+”, "
+OilChangeList.FilterBrand+", "+OilChangeList.FilterPrice+", "+OilChangeList.OilFilterPurchaseLocation);

* string csvpath= “D:\xyz.csv”;*
* File.AppendAllText(csvpath, csvcontent.ToString());*
Thanks in advance for the help!

Sorry but your question is missing pretty much all important information. Also this makes no sense whatsoever:

foreach(JSONNode oc in oilchangelist)
    oilchangelist.Add (oc);

You iterate over the nodes in “oilchangelist” (which probably is a JSONArray) and for each element in the collection you add the same element to the same collection again. Lucky for you the internally used List does not allow modifing the list (adding / removing) when iterating through an Enumerator.

We don’t know how your JSON data looks like and what data it contains. Also we don’t know what your CSV output should look like.

Assuming your json looks something like this:


and your output should look like this:

Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price, Filter Location

You would just do:

var data = JSON.Parse(yourJSONText);
StringBuilder csv = new StringBuilder();
csv.AppendLine("Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price, Filter Location");
foreach(JSONNode n in data)

string outputCSV = csv.ToString();

Be warned that “AppendAllText” will append to the end of the file and keeps the current content at the start. This makes no sense in your case since you write a new CSV header. So appending makes no sense. You probably want to use WriteAllText which will replace all content in the target file.

Note that if one of your fields actually contain problematic characters (which are the comma, new line characters as well as double quotes) the field should be enclosed in double quotes and contained quotes should be duplicated. This one might help

If that’s not what you wanted to do, you have to edit your question and add the relevant details I’ve mentioned at the beginning.