- 沒有新回應!
C# & Json-P2(進階款)
@@ 看不太懂..有感json愈來愈流行...不過先記錄起來...
using System.Collections.Generic;
using System.Web.Script.Serialization;
private Dictionary<string, object> ReadJsonInput(string inputJson)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
JavaScriptSerializer jss = new JavaScriptSerializer();
object obj = jss.DeserializeObject(inputJson);
dict = (Dictionary<string, object>)obj;
return dict;
}
protected void Page_Load(object sender, EventArgs e)
{
string strGra="";
strGra += "{";
strGra += " 'graphics':";
strGra += " [";
strGra += " {";
strGra += " 'geometry':{'x':296788,'y':2766834,'spatialReference':{'wkid':102443}},";
strGra += " 'symbol':{'color':[0,0,0,255],'size':13,'angle':0,'xoffset':0,'yoffset':0,'style':'gra','url':'A.gif'},";
strGra += " 'infoTemplate':{'title':'標題A','content':'TIP訊息A'}";
strGra += " },";
strGra += " {";
strGra += " 'geometry':{'x':296080,'y':2767295,'spatialReference':{'wkid':102443}},";
strGra += " 'symbol':{'color':[0,0,0,255],'size':13,'angle':0,'xoffset':0,'yoffset':0,'style':'gra','url':'B.gif'},";
strGra += " 'infoTemplate':{'title':'標題B','content':'TIP訊息B'}";
strGra += " },";
strGra += " {";
strGra += " 'geometry':{'x':296335,'y':2766921,'spatialReference':{'wkid':102443}},";
strGra += " 'symbol':{'color':[0,0,0,255],'size':13,'angle':0,'xoffset':0,'yoffset':0,'style':'gra','url':'C.gif'},";
strGra += " 'infoTemplate':{'title':'標題C','content':'TIP訊息C'}";
strGra += " }";
strGra += " ]";
strGra += "}";
Dictionary<string, object> dictG = new Dictionary<string, object>();
dictG = ReadJsonInput(strGra);
Dictionary<string, object>.ValueCollection dictG2 = dictG.Values;
foreach (Object[] dictG3 in dictG2)
{
foreach (Dictionary<string, object> dictItem in dictG3)
{
Object dgxy = (Object)dictItem;
Object x = ((Dictionary<string, object>)((Dictionary<string, object>)dgxy)["geometry"])["x"];
Object y = ((Dictionary<string, object>)((Dictionary<string, object>)dgxy)["geometry"])["y"];
Object url = ((Dictionary<string, object>)((Dictionary<string, object>)dgxy)["symbol"])["url"];
Object title = ((Dictionary<string, object>)((Dictionary<string, object>)dgxy)["infoTemplate"])["title"];
Response.Write("x=" + x.ToString() + ",y=" + y.ToString() + ",url = " + url.ToString()+",title=" + title.ToString() + "<br>");
}
}
}


