English 中文(简体)
• 如何将json的海面化
原标题:how to deserialize json in asp.net

我遵循的是要求上网的守则。

StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.bigflix.com/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435&timestamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd&nbsp&nbsp<br>");
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
Stream resstream = responce.GetResponseStream();
string tempString = null;
int count = 0;
do
{
    count = resstream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
        tempString = Encoding.ASCII.GetString(buf, 0, count);
        sb.Append(tempString);
    }
}
while (count > 0);
{
    Response.Write(sb.ToString() + "<br/><br/>");
    // string[] val = sb.ToString().Split( " );
}

该法典实施后,将获得此类json。

[
    { "id": 23, "name": "Video Clips" }, 
    { "id": 15, "name": "Deleted Scenes" }, 
    { "id": 9, "name": "Music Albums" }, 
    { "id": 7, "name": "Trailers" }, 
    { "id": 18, "name": "Short Films" }, 
    { "id": 21, "name": "Movie Clips" }, 
    { "id": 1, "name": "Movies " }, 
    { "id": 4, "name": "Plays" }, 
    { "id": 22, "name": "Scenes" }, 
    { "id": 2,  "name": "TV Show" }, 
    { "id": 5, "name": "Kids" }, 
    { "id": 16, "name": "Interviews" }, 
    { "id": 11, "name": "Film Songs" }, 
    { "id": 14, "name": "Making of Movie" }
]

Now i want deserialize this in asp.net(c#)
I tried to get a proper answer but didn t get.

请提出咨询意见。

最佳回答

创立一个称为“从Flix inseide App_Code”的班级。

public class FromFlix
{
    public string ID { get; set; }
    public string Name { get; set; }
}

现在,在你去世之后,我们就是这样做的。

JavaScriptSerializer ser = new JavaScriptSerializer();
var response = ser.Deserialize<IList<FromFlix>>(sb.ToString());

The response is a List<FromFlix>, that is, a generic list of type FromFlix
This is how you should use it.

StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.bigflix.com/BIGFlixApi.do?parameter=getProductType&partnerID=17&uniqueID=54325345435&timestamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd&nbsp&nbsp<br>");
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
Stream resstream = responce.GetResponseStream();
string tempString = null;
int count = 0;
do
{
    count = resstream.Read(buf, 0, buf.Length);
    if (count != 0)
    {
        tempString = Encoding.ASCII.GetString(buf, 0, count);
        sb.Append(tempString);
    }
}
while (count > 0);
JavaScriptSerializer ser = new JavaScriptSerializer();
List<FromFlix> response = ser.Deserialize<List<FromFlix>>(sb.ToString());
foreach (var item in response)
{
    Response.Write("ID: " + item.ID + "&" + "Name: " + item.Name + "<br/>");
}

希望这一帮助。

问题回答

您可使用JavagustSerializer, 类型可序列化和淡化JSON数据。

var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<TheTypeToWhichJSONWillMap>(myJson);

EDIT:

我不敢肯定你会遇到什么问题,但以下是同青年领袖一道工作的例子:

static string TheJson = "...";

public class TheType
{
    public int id { get; set;}
    public string name { get; set; }
}

var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<List<TheType>>(TheJson);

这使得我们有<条码>代号<>代号/编码>,成为<条码>;Type>,有14项内容。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签