我已经解决了将一些JSON数据解析为原始对象数组的问题,这里有什么意思 我有这个原始数据(JavaScript),例如:
var points = [];
points = [
{ lat: "38.713852", lng: "-9.145621", data: "<div align=left class=gMapsOverlay><span>Loja centro de Braga</span></div>", options: { icon: '../layout/icon_map_point.png' } },
{ lat: "39.825751", lng: "-7.479642", data: "<div align=left class=gMapsOverlay><span>Apartamento T3</span></div>", options: { icon: '../layout/icon_map_point.png' } },
{ lat: "38.730638", lng: "-9.139604", data: "<div align=left class=gMapsOverlay><span>Apartamento Duplex</span></div>", options: { icon: '../layout/icon_map_point.png' } }
];
我必须这样做,因为我试图解释,我需要将它发送到ID =“hdd_GmapPoints”的隐藏字段(在这种情况下),我这样做:
private void SetMap () {
string aux = "";
string[] lat = new string[3];
string[] lng = new string[3];
string[] designacao = new string[3];
lat[0] = "38.713852";
lat[1] = "39.825751";
lat[2] = "38.730638";
lng[0] = "-9.145621";
lng[1] = "-7.479642";
lng[2] = "-9.139604";
designacao[0] = "Loja centro de Braga";
designacao[1] = "Apartamento T3";
designacao[2] = "Apartamento Duplex";
for(int i = 0; i < lat.Length; i++) {
aux += "{ lat: \"" + lat[i].ToString().Replace(",", ".") + "\", " +
"lng: \"" + lng[i].ToString().Replace(",", ".") + "\", " +
"data: \"<div align=left class=gmapsoverlay><span>" + designacao[i] + "</span></div>\", " +
"options: { icon: '../layout/icon_map_point.png' } },";
//aux[idx] = "{ lat: \"" + r["latitude"].ToString().Replace(",", ".") + "\", " +
// "lng: \"" + r["longitude"].ToString().Replace(",", ".") + "\", " +
// "data: '<div align=left class=gMapsOverlay><span>" + r["designacao"] + "</span></div>', " +
// "options: { icon: '../layout/icon_map_point.png' } }";
}
if(aux != "") {
//hdd_GmapPoints.Value = aux.ToString();
//hdd_GmapPoints.Value = aux.Substring(0, aux.Length - 1);
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(aux.ToString().Substring(0, aux.ToString().Length - 1));
hdd_GmapPoints.Value = json;
}
}
然后我必须将它解析回Javascript对象数组,这是我的javascript代码:
function setMap() {
var points = [];
points = [
{ lat: "38.713852", lng: "-9.145621", data: "<div align=left class=gMapsOverlay><span>Loja centro de Braga</span></div>", options: { icon: '../layout/icon_map_point.png' } },
{ lat: "39.825751", lng: "-7.479642", data: "<div align=left class=gMapsOverlay><span>Apartamento T3</span></div>", options: { icon: '../layout/icon_map_point.png' } },
{ lat: "38.730638", lng: "-9.139604", data: "<div align=left class=gMapsOverlay><span>Apartamento Duplex</span></div>", options: { icon: '../layout/icon_map_point.png' } }
];
//points = [str1.split('%')];
var aux = $("#<%=hdd_GmapPoints.ClientID %>").val();
var points2 = eval(aux);
//var points2 = JSON.parse(aux);
console.log("TYPE 1: " + $.type(points));
console.log("CONTENT 1:" + points);
console.log("TYPE 2: " + $.type(points2));
console.log("CONTENT 2:" + points2);
console.table(points);
}
但它不起作用,因为它向我展示了supose对象作为字符串,这是我从console.log(points2)得到的;
“{lat:”38.713852“,lng:” - 9.154521“,数据:”Loja centro de Braga“,选项:{icon:'../layout/icon_map_point.png'}},{lat:”39.825751 “,lng:” - 7.179642“,数据:”Apartamento T3“,选项:{icon:'.. / layout / icon_map_png'}},{lat:”38.730638“,lng:” - 9.19604“,数据: “Apartamento Duplex”,选项:{icon:'.. / layout / icon_point.png'}}“
我需要这个给出什么
console.table(points);
给出
我再次需要将隐藏字段从c#发送到JQuery,代码必须通过该隐藏字段。
有人知道吗?
答案 0 :(得分:1)
很抱歉大家花时间,但我找到了解决方案,因为我不擅长c#我没想到这会起作用但是有效。
我所做的就是在代码后面创建对象,这样我就可以完全将整个对象从c#传递到带有JSON的javascript。
//declared this 2 classes at the top
class Options {
public string icon {
get;
set;
}
}
class Aux {
public string lat {
get;
set;
}
public string lng {
get;
set;
}
public string data {
get;
set;
}
public Options options {
get;
set;
}
}
private void SetMap () {
Options opt = new Options();
List<Aux> aux = new List<Aux>();
//this is to make the code to work with dinamic content
string[] latitude = new string[3];
string[] longitude = new string[3];
string[] designacao = new string[3];
latitude[0] = "38.713852";
latitude[1] = "39.825751";
latitude[2] = "38.730638";
longitude[0] = "-9.145621";
longitude[1] = "-7.479642";
longitude[2] = "-9.139604";
designacao[0] = "Loja centro de Braga";
designacao[1] = "Apartamento T3";
designacao[2] = "sxpto";
for (int i=0; i< 3; i++){
opt.icon = "../layout/icon_map_point.png";
//this is what it's really needed
aux.Add(new Aux {lat = latitude[i], lng = longitude[i], data = "div align=left class=gmapsoverlay><span>"+designacao[i]+"</span></div>", options = opt});
}
// end of dinamic content
if(aux != null) {
//parse the whole object to JSON
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = jsonSerializer.Serialize(aux);
hdd_GmapPoints.Value = json;
}
}
在javascript方面,只需要解码隐藏字段
var aux = $("#<%=hdd_GmapPoints.ClientID %>").val();
var points2 = JSON.parse(aux);