CONGRATULATIONS! I need to deserialize a file of different objects serialized as json. Here is the resulting file:
{ "Number": 1, "Description": "Run version with strategy data", "Context": "NA" }[ { "N": 0.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.0, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 1.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.2999999523162842, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 2.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.0, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 3.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.2999999523162842, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 4.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.0, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 5.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.2999999523162842, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 6.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.0, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 7.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.2999999523162842, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 8.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.0, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" }, { "N": 9.0, "T": 2.0, "Adc": [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], "SpltFr": 2.2999999523162842, "Acc": 1.0, "DAcc": 0.0, "Acc2": 1.0, "OscFr": 0.5, "Fltr": 0, "CmpEr": false, "ErrPck": 0, "IndxDiff": 0, "Pos": 0, "FastAcc": [], "GIndx": 0, "Indx": 0, "PcTime": "0001-01-01T00:00:00" } ]
I am using json.net, but it does not seem to work the way I want.
StreamReader reader = new StreamReader(fileName, Encoding.GetEncoding(1251)); var serializer = new JsonSerializer() { CheckAdditionalContent = false }; var obj1 = serializer.Deserialize(reader, typeof(Type1)); var obj2 = serializer.Deserialize(reader, typeof(Type2));
This code reads only the first object and gives an exeption when reading the second. What am I doing wrong? Can json.net complete my task? Thanks in advance!
Taking some tips from this thread, I changed my code:
using (FileStream fs = File.Open(fileName, FileMode.Open)) using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(1251))) using (JsonTextReader jr = new JsonTextReader(sr)) { JsonSerializer serializer = new JsonSerializer() { Formatting = Newtonsoft.Json.Formatting.Indented, CheckAdditionalContent = false, TypeNameHandling = TypeNameHandling.Auto }; var data1 = serializer.Deserialize(jr, typeof(Type1)); var data2 = serializer.Deserialize(jr, typeof(List<Type2>)); }
But it still does not work correctly. It seems that after reading the first object, my cursor does not stop immediately at the end of it, but somewhere in the second object. Therefore, my reader can no longer read any objects.