Reputation: 1
I created a WCF service to return JSON, however, it is not returned in the way i need it.
My interface:
[ServiceContract]
public interface IService1
{
[OperationContract(Name="Sensors")]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "test")]
List<Sensor> testdata();
}
My service:
public class Service1 : IService1
{
public List<Sensor> testdata()
{
return HamsData.GetSensorDetails("1");
}
}
HamsData.GetSensorDetails:
public static List<Sensor> GetSensorDetails(string id)
{
List<Sensor> al = new List<Sensor>();
DateTime now = DateTime.Now;
DateTime thishour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0);
using ( HAMSDataClassesDataContext db = new HAMSDataClassesDataContext())
{
var dbsensors = (from p in db.sensordetails where p.sensorlatestdate == thishour select new Sensor { name = p.sensor.name, value = p.sensorlatestvalue });
foreach (Sensor x in dbsensors)
{
al.Add(x);
}
return al;
}
}
The Sensor class:
[Serializable]
[DataContract(Name = "mysensor")]
public class Sensor
{
[DataMember]
public string name { get; set; }
[DataMember]
public decimal value { get; set; }
}
This return the following JSON:
[{"name":"EIC","value":1000.000},{"name":"GIC","value":0.000},{"name":"WIC","value":0.000},{"name":"EHC","value":0.010},{"name":"GHC","value":0.000},{"name":"WHC","value":0.000},{"name":"EDC","value":33458.560},{"name":"ENC","value":27450.040},{"name":"GRC","value":35227.100},{"name":"WRC","value":38.390},{"name":"ECR","value":1.000},{"name":"10:D8:A2:DC:01:08:00:E0:T","value":24.120},{"name":"10:94:3D:B4:01:08:00:BC:T","value":46.310},{"name":"10:31:85:70:01:08:00:4D:T","value":20.940},{"name":"10:5D:5E:B4:01:08:00:EA:T","value":7.690},{"name":"10:DD:56:B4:01:08:00:3E:T","value":17.690},{"name":"28:51:32:5D:02:00:00:0A:T","value":12.560},{"name":"26:B1:08:81:00:00:00:39:T","value":15.030},{"name":"26:B1:08:81:00:00:00:39:H","value":-29.890},{"name":"26:CD:A0:6D:00:00:00:8A:T","value":15.030},{"name":"26:CD:A0:6D:00:00:00:8A:L","value":204.600}]
However, I want it without the name and value tags like:
[{"EIC":1000.000},{"GIC":0.000},{"WIC":0.000},{"EHC":0.010},{"GHC":0.000},{"WHC":0.000},{"EDC":33458.560},{"ENC":27450.040},{"GRC":35227.100},{"WRC":38.390},{"ECR":1.000},{"10:D8:A2:DC:01:08:00:E0:T":24.120},{"10:94:3D:B4:01:08:00:BC:T":46.310},{"10:31:85:70:01:08:00:4D:T",e":20.940},{"10:5D:5E:B4:01:08:00:EA:T":7.690},{"10:DD:56:B4:01:08:00:3E:T":17.690},{"28:51:32:5D:02:00:00:0A:T"12.560},{"26:B1:08:81:00:00:00:39:T":15.030},{"26:B1:08:81:00:00:00:39:H":-29.890},{"26:CD:A0:6D:00:00:00:8A:T":15.030},{"26:CD:A0:6D:00:00:00:8A:L":204.600}]
Can someone tell what i'm doing wrong here?
Upvotes: 0
Views: 367
Reputation: 6021
Thats not json, thats the problem. Json encoding is a combination of property/value. You are trying to misuse it.
Are you really sure you want to encode the data that way? It makes it far hard to extract the values.
Upvotes: 1