- Home /
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>() ;
OilChangeList.Add(CSVList.ToList());
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[i].ServiceDate+", "+OilChangeList[i].Location+", "+OilChangeList[i].Mileage+"," +
" "+OilChangeList[i].Labor+", "+OilChangeList[i].OilBrand+", "+ OilChangeList[i].OilPrice+", "
+OilChangeList[i].FilterBrand+", "+OilChangeList[i].FilterPrice+", "+OilChangeList[i].OilFilterPurchaseLocation);
}
string csvpath= "D:\\xyz.csv";
File.AppendAllText(csvpath, csvcontent.ToString());
Thanks in advance for the help!
Answer by Bunny83 · Jun 21, 2019 at 01:15 AM
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:
[
{
"ServiceDate":"aaa",
"Location":"bbb",
"Mileage":"ccc",
"Labor":"ddd",
"OilBrand":"eee",
"OilPrice":"fff",
"FilterBrand":"ggg",
"FilterPrice":"hhh",
"OilFilterPurchaseLocation":"iii"
},
{
"ServiceDate":"aaa2",
"Location":"bbb2",
"Mileage":"ccc2",
"Labor":"ddd2",
"OilBrand":"eee2",
"OilPrice":"fff2",
"FilterBrand":"ggg2",
"FilterPrice":"hhh2",
"OilFilterPurchaseLocation":"iii2"
}
]
and your output should look like this:
Service Date, Location, Mileage, Labor, Oil Brand, Oil Price, Filter Brand, Filter Price, Filter Location
aaa,bbb,ccc,ddd,eee,fff,ggg,hhh,iii
aaa2,bbb2,ccc2,ddd2,eee2,fff2,ggg2,hhh2,iii2
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)
{
csv.Append(n["ServiceDate"].Value).Append(",");
csv.Append(n["Location"].Value).Append(",");
csv.Append(n["Mileage"].Value).Append(",");
csv.Append(n["Labor"].Value).Append(",");
csv.Append(n["OilBrand"].Value).Append(",");
csv.Append(n["OilPrice"].Value).Append(",");
csv.Append(n["FilterBrand"].Value).Append(",");
csv.Append(n["FilterPrice"].Value).Append(",");
csv.Append(n["OilFilterPurchaseLocation"].Value).AppendLine();
}
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.
Thanks! I apologize for my incompleteness. Here is the json as it comes over from the server:
[{"VehicleID":"2G1FT1EW8B915261","ServiceDate":"2017-03-09","ServiceLocation":"Granite Town","Labor":"15","$$anonymous$$ileage":"47000","OilBrand":"Great Value 5w30","OilPrice":"1.99","FilterBrand":"Fram","FilterPrice":"2.95","PurchasePlace":"Walmart"},{"VehicleID":"2G1FT1EW8B915261","ServiceDate":"2017-08-11","ServiceLocation":"Granite Town","Labor":"15","$$anonymous$$ileage":"52000","OilBrand":"Great Value 5W30","OilPrice":"2.35","FilterBrand":"Fram Xtra","FilterPrice":"3.97","PurchasePlace":"Walmart"},{"VehicleID":"2G1FT1EW8B915261","ServiceDate":"2018-01-07","ServiceLocation":"","Labor":"0","$$anonymous$$ileage":"56000","OilBrand":"Great Value 5W30","OilPrice":"1.99","FilterBrand":"Fram","FilterPrice":"2.95","PurchasePlace":"Walmart"}]
So I can use the jsonarray and don't have to convert it to a list?
Now it's giving an error: Assets/Scripts/TestScript1.cs(21,16): error CS0021: Cannot apply indexing with [] to an expression of type `System.Collections.Generic.$$anonymous$$eyValuePair' Do I need to convert it to something else before using it? The compiler is not liking the Value parameter- csv.Append(n["ServiceDate"].Value).Append(","); Further help please.
Ohh, I'm sorry. I just forgot to specify the type in the foreach loop. You should do this:
foreach(JSONNode n in data)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Help, Reading a Json file (getting error KeyNotFounfExeption) 1 Answer
Json.net - windows store phone 8.1 2 Answers