我已按照网页上的请求
进行操作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×tamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd  <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" }
]
现在我想在asp.net(c#)中反序列化这个 我试图得到一个正确的答案,但没有得到。
请建议。
答案 0 :(得分:11)
像这样创建一个名为FromFlix的类App_Code
public class FromFlix
{
public string ID { get; set; }
public string Name { get; set; }
}
现在,在while循环结束后,执行此操作。
JavaScriptSerializer ser = new JavaScriptSerializer();
var response = ser.Deserialize<IList<FromFlix>>(sb.ToString());
response
是List<FromFlix>
,即FromFlix类型的通用列表
这就是你应该如何使用它。
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×tamp=131286916367&digest=bf53cae8f364cfc1d796489d09e4cfd  <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/>");
}
希望这有帮助。
答案 1 :(得分:4)
您可以使用JavaScriptSerializer
类型序列化和反序列化JSON数据。
var serializer = new JavaScriptSerializer();
var deserialized = serializer.Deserialize<TheTypeToWhichJSONWillMap>(myJson);
编辑:
我不确定您对此有什么问题,但以下是您提供的JSON字符串的工作示例:
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);
这使我们deserialized
成为List<TheType>
,包含14个元素。
答案 2 :(得分:2)
你需要的是JSON.NET检查这个链接http://json.codeplex.com/会让你的生活更轻松