Click here to Skip to main content
14,928,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:-
JavaScript
[{
	"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:
JavaScript
[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:-
JavaScript
{
	"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:-
C#
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:-
C#
dynamic vx = Newtonsoft.Json.JsonConvert.DeserializeObject(strjson);

C#
Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0.


What I have tried:

C#
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
    { }
}
}
Posted
Updated 9-Dec-16 16:19pm
v2

1 solution

I have feel so bad where every time I post for question that I've been searching quite some times, I found the way for my own questions.

To read the original format that been supplied by client:-
{
	"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"
}

Is can be done by this:-
C#
string[] jsonFileContent = File.ReadAllLines(jsonfile[i].ToString());
foreach (string value in jsonFileContent)
{
    Console.WriteLine(value);
}


I found this answer in here[^] .
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900