//下载LitJSON.dll:http://download.csdn.net/detail/pukuimin1226/5851711
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LitJson;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
//可到 http://sourceforge.net/projects/litjson/?source=dlp 下载
protected void Page_Load(object sender, EventArgs e)
{
String str = "{'name':'j1','id':1,'items':[{'itemid':1001,'itemname':'it1'},{'itemid':1002,'itemname':'it2'}]}";
//*** 读取JSON字符串中的数据 *******************************
JsonData jd = JsonMapper.ToObject(str);
String name = (String)jd["name"];
int id = (int)(jd["id"]);
Response.Write(string.Format("name:{0},id:{1}<br/>",name,id));
JsonData jdItems = jd["items"];
Response.Write(string.Format("items:<br/>"));
int itemCnt = jdItems.Count;
// 数组 items 中项的数量
foreach (JsonData item in jdItems)
// 遍历数组 items
{
int itemID = (int)item["itemid"];
String itemName = (String)item["itemname"];
Response.Write(string.Format("itemid:{0},itemname:{1}<br/>", itemID, itemName));
}
ps ps1 = new ps();
ps1.name = "ps_name1";
ps1.height = 168;
string json_ps1 = JsonMapper.ToJson(ps1);
Response.Write(json_ps1+"<br/>");
ps ps2 = JsonMapper.ToObject<ps>(json_ps1);
Response.Write(string.Format("ps2_name:{0}<br/>ps2_height:{1}<br/>",ps2.name,ps2.height));
}
}
public class ps
{
public string name { get; set; }
public int height { get; set; }
}
}