我想把我的数据转换成所需的JSON格式。我的数据长这样:
[
"{
id:001,
name:akhilesh,
}",
"{
id:002,
name:Ram,
}"
]
我希望将上述数据转换为有效的JSON:
[
{
"id":"001",
"name":"akhilesh"
},
{
"id":"002",
"name":"Ram"
}
]
我尝试了以下方法,但都没有帮助:
JSON.serialize
JSON.parse
eval
服务器端返回的数据响应如下:
{
"d": [
"{id:413,title:ranjan,start:413,end:413}",
"{id:414,title:raja,start:414,end:414}",
"{id:415,title:raja g,start:415,end:415}",
"{id:416,title:abhh,start:416,end:416}",
"{id:417,title:chta,start:417,end:417}",
"{id:418,title:Raju,start:418,end:418}",
"{id:419,title:Ranjan,start:419,end:419}",
"{id:420,title:Raja,start:420,end:420}",
"{id:421,title:chitti,start:421,end:421}",
"{id:422,title:Raja,start:422,end:422}",
"{id:423,title:raja,start:423,end:423}",
"{id:424,title:yash,start:424,end:424}",
"{id:425,title:vsg,start:425,end:425}",
"{id:431,title:Vimal11,start:431,end:431}",
"{id:432,title:Aruhi,start:432,end:432}",
"{id:434,title:Aruhi,start:434,end:434}",
"{id:435,title:,start:435,end:435}",
"{id:436,title:xs,start:436,end:436}",
"{id:437,title:rajkj,start:437,end:437}",
"{id:438,title:mmt,start:438,end:438}",
"{id:439,title:xaxa,start:439,end:439}",
"{id:440,title:yash,start:440,end:440}"
]
}
服务器端代码
[System.Web.Services.WebMethod]
public static List<string> getData()
{
List<string> data = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=ACME-PC\\SQL;Integrated Security=true;Initial Catalog=ClinicApplication"))
{
SqlCommand cmd = new SqlCommand("select DISTINCT Patient_ID,First_Name,fromtime,totime,Date from tbl_AddPatientInfo", con);
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string id = "{" +
"\"id:\"" + dr["Patient_ID"].ToString() + "," +
"title:" + dr["First_Name"].ToString() + "," +
"start:" + dr["Patient_ID"].ToString() + "," +
"end:" + dr["Patient_ID"].ToString() +
"}";
string ids = id.Replace(@"""", "");
data.Add(ids);
}
return data;
}
}
}
title
对于415包含空格,而对于435则不存在。如果无法更改数据格式为JSON,则至少使数据保持一致。 - Tushar