Hi,
How to retrieve data from json file that have multiple line of data? Currently what I'm trying can be refer in "What I've tried"
Below are the sample data:-
[{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "c",
"Timestamp": "2016-12-8T13:54:12+08:00",
"Duration": "0.000086712"
},
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "3",
"Timestamp": "2016-12-8T13:54:12+08:00",
"Duration": "0.0"
},
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "a",
"Timestamp": "2016-12-8T13:54:11+08:00",
"Duration": "0.000100080"
},
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "5",
"Timestamp": "2016-12-8T13:54:10+08:00",
"Duration": "0.0"
}]
When I'm stepping into the datajson, I can view the properties inside the object and see all sample data. But, currently the data is split correctly "Count = 4" but when I step inside the count = 4, I see another 5 data property. What I want is get every json array with lambda split by break. As result, in datajson array count, it has the value of below without deserialized into each variables:
[0] {"DataType": "Spectre_DT","HostName": "spectrum","Listener": "c","Timestamp": "2016-12-8T13:54:12+08:00","Duration": "0.000086712"}
[1] {"DataType": "Spectre_DT","HostName": "spectrum","Listener": "3","Timestamp": "2016-12-8T13:54:12+08:00","Duration": "0.0"}
[2] {"DataType": "Spectre_DT","HostName": "spectrum","Listener": "a","Timestamp": "2016-12-8T13:54:11+08:00","Duration": "0.000100080"}
[3] {"DataType": "Spectre_DT","HostName": "spectrum","Listener": "5","Timestamp": "2016-12-8T13:54:10+08:00","Duration": "0.0"}
Original data is in below format:-
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "c",
"Timestamp": "2016-12-8T13:54:12+08:00",
"Duration": "0.000086712"
}
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "3",
"Timestamp": "2016-12-8T13:54:12+08:00",
"Duration": "0.0"
}
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "a",
"Timestamp": "2016-12-8T13:54:11+08:00",
"Duration": "0.000100080"
}
{
"DataType": "Spectre_DT",
"HostName": "spectrum",
"Listener": "5",
"Timestamp": "2016-12-8T13:54:10+08:00",
"Duration": "0.0"
}
So the code I'm using to deserialize object turns error:-
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'jsonreader.SpectrumData[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'DataType', line 1, position 12.
If I'm Deserialized to dynamic object without using class library, this error will raise:-
dynamic vx = Newtonsoft.Json.JsonConvert.DeserializeObject(strjson);
Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.
What I have tried:
public class SpectrumData
{
public string DataType { get; set; }
public string HostName { get; set; }
public string Listener { get; set; }
public string Timestamp { get; set; }
public double Duration { get; set; }
}
class Program
{
static void Main(string[] args)
{
try
{
var strjson = File.ReadAllText(jsonfile[i].ToString());
SpectrumData[] datajson = JsonConvert.DeserializeObject<SpectrumData[]>(strjson);
}
catch
{ }
}
}