I am using the Javascript serializer to parse a JSON file. Its runs fine when the file is in a valid json format but failing for instance there is a extra comma in the last field. How could I bypass that, I am only retrieving the replicateid from this file:
{
"Orders":
[
{
"Rack": "0014",
"SampleType": "Calibrator",
"Replicate": 3,
"Track": 1,
"Lane": 2,
"ReagentMasterLot": "06100AA02",
"ReagentSerialNumber": "60002",
"Comment": "HTLV Cal T1L2",
}
]
}
public static KeyValuePair<bool, int> CyclesCompleted(string fileName)
{
int cyclesCompleted = 0;
JavaScriptSerializer ser = jss();
bool isValid = true;
try
{
CategoryTypeColl ctl = ser.Deserialize<CategoryTypeColl>(LoadTextFromFile(fileName));
if (ctl != null)
{
List<CategoryType> collection = (from item in ctl.orders
select item).ToList();
foreach (var replicates in collection)
{
cyclesCompleted = cyclesCompleted + replicates.Replicate;
}
}
}
catch
{
isValid = false;
}
return new KeyValuePair<bool, int>(isValid, cyclesCompleted);
}
You should use JSON.NET. it is an open source library to serialize and deserialize the .net objects to json string and vice versa. It almost solve these kind of issues which .net json serializiation not.