我们有一个Web API,它从oracle数据库中选择记录并以JSON格式返回结果。表中的字段很少是Number数据类型。因此,在JSON响应中,Number数据类型的字段具有小数位,如123.0。有没有办法我们可以得到整数值并省略小数位,如123
public class DataController : ApiController
{
[HttpGet]
public HttpResponseMessage Getdetails(string id, DateTime date_in)
{
var prms = new List<OracleParameter>();
prms.Add(new OracleParameter("id", OracleDbType.Varchar2, id, ParameterDirection.Input));
prms.Add(new OracleParameter("date_in", OracleDbType.Date, date_in, ParameterDirection.Input));
var connStr = ConfigurationManager.ConnectionStrings["DtConnection"].ConnectionString;
using (OracleConnection dbconn = new OracleConnection(connStr))
{
DataSet userDataset = new DataSet();
var strQuery = "SELECT REQUEST_ID from SAMPLE where id = :id and date_in = :date_in ";
var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition))
{
response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;
}
}
}
我们有两个用于处理JSON的类
[JsonConverter(typeof(OracleDataTableJsonResponseConverter))]
public sealed class OracleDataTableJsonResponse
{
public string ConnectionString { get; private set; }
public string QueryString { get; private set; }
public OracleParameter[] Parameters { get; private set; }
public OracleDataTableJsonResponse(string connStr, string strQuery, OracleParameter[] prms)
{
this.ConnectionString = connStr;
this.QueryString = strQuery;
this.Parameters = prms;
} }
public class OracleDataTableJsonResponseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(OracleDataTableJsonResponse);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("OracleDataTableJsonResponse is only for writing JSON. To read, deserialize into a DataTable");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var response = (OracleDataTableJsonResponse)value;
using (var dbconn = new OracleConnection(response.ConnectionString))
{
dbconn.Open();
using (var selectCommand = new OracleCommand(response.QueryString, dbconn))
{
if (response.Parameters != null)
selectCommand.Parameters.AddRange(response.Parameters);
using (var reader = selectCommand.ExecuteReader())
{
writer.WriteDataTable(reader, serializer);
} } }}}
public static class JsonExtensions
{
public static void WriteDataTable(this JsonWriter writer, IDataReader reader, JsonSerializer serializer)
{
if (writer == null || reader == null || serializer == null)
throw new ArgumentNullException();
writer.WriteStartArray();
while (reader.Read())
{
writer.WriteStartObject();
for (int i = 0; i < reader.FieldCount; i++)
{
writer.WritePropertyName(reader.GetName(i));
serializer.Serialize(writer, reader[i]);
}
writer.WriteEndObject();
}
writer.WriteEndArray();
JSON结果类似于
{"data":[{"REQUEST_ID":333838.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":359544.0}]}
在Oracle DB中,该字段看起来像
是否有可能选择Number数据类型的字段省略小数位。
答案 0 :(得分:1)
您需要在序列化值
的代码中更新以下代码块for (int i = 0; i < reader.FieldCount; i++)
{
writer.WritePropertyName(reader.GetName(i));
serializer.Serialize(writer, reader[i]);
}
如下所示,
for (int i = 0; i < reader.FieldCount; i++)
{
string name =reader.GetName(i);
writer.WritePropertyName(name);
if(name == "REQUEST_ID")
{
serializer.Serialize(writer, string.Format("{0:n0}",reader[i]));
}else
{
serializer.Serialize(writer, reader[i]);
}
}
答案 1 :(得分:0)
更改此查询行,如下所示:
DataSet userDataset = new DataSet();
var strQuery = "SELECT CAST(REQUEST_ID AS int) from SAMPLE where id = :id and date_in = :date_in ";