我有一组这样的组件:
var names =
1)"lat: 40.6447077, lng: -73.878421, address: 1600 Pennsylvania Avenue, Brooklyn, NY 11239, USA"
2)"lat: 40.609099, lng: -73.931516, address: 2015 E. 35th street, Brooklyn, Ny, United States"
我试图将其解析为一个对象数组。以下代码非常适合解析纬度和经度,但在尝试解析地址时也会收到意外的令牌错误。
var newArray = names.map(function (str) {
return JSON.parse("{" + str.replace(/lat/, '"lat"').replace(/lng/, '"lng"').replace(/address/, '"address"').replace(/;/, "") + "}")
});
答案 0 :(得分:2)
JSON中的字符串必须是双引号。
使用try
{
using (SqlConnection cn = new SqlConnection(@"Data Source=.; Initial Catalog=Employee1;Persist Security Info=True;User ID=sa;Password=786"))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.Date";
cmd.Parameters.AddWithValue("@DOB", Convert.ToDateTime(textBox1.Text));
using (DataTable dt = new DataTable())
{
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Employee ID", typeof(string)), new DataColumn("Employee Name", typeof(string)), new DataColumn("Status", typeof(char)) });
cn.Open();
using (SqlDataReader res = cmd.ExecuteReader())
{
MessageBox.Show("COmmand Executed Successfully");
while(res.Read())
{
dt.Rows.Add(res[0].ToString(), res[1].ToString(), res[2].ToString());
}
dataGridView1.DataSource = dt;
}
}
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.StackTrace);
}
(/address/, '"address"')
答案 1 :(得分:0)
names.map(function (str) {
return JSON.parse(
"{" + str.replace(/address: /, 'address: "').replace(/(\w+): /g, '"$1": ')+'"}'
);
});
您首先要将地址日期包含在引号内,因为它不仅是文本,还包含逗号。混乱嘉豪。然后你也可以在引号内:
之前包装单词。