asp.net 3.5 Web应用程序中的json对象 [英] json object in asp.net 3.5 web application

查看:90
本文介绍了asp.net 3.5 Web应用程序中的json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络应用程序中使用json文件检索json文件提供的信息.Json文件的网址为 http: //json-cricket.appspot.com/score.json [ ^ ]
我尝试了如下代码:

I am using json file in my web aplication to retrive information provided by json file.Url for the Json file is http://json-cricket.appspot.com/score.json[^]
i tried code which is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

public void ReadJsonfile()
{
String strResult;
            WebResponse objResponse;
            WebRequest objRequest = HttpWebRequest.Create("http://json-cricket.appspot.com/score.json");
            objResponse = objRequest.GetResponse();
            using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
            {
                strResult = sr.ReadToEnd();
                sr.Close();
            }
           
            JavaScriptSerializer ser = new JavaScriptSerializer();
            
           // MyObject deserializedObject = (MyObject)JavaScriptConvert.DeserializeObject(strJson, typeof(MyObject));
            Response.Write(strResult);
            ICCWorldMatch ic = new ICCWorldMatch();
           var l = JsonConvert.DeserializeObject(strResult);
}


我上一堂课:


I have one class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace testRSSFeed
{
    public class ICCWorldMatch
    {
        public string Batting_Team { set; get; }
        public string Date { set; get; }
        public string Match { set; get; }
        public string score { set; get; }
        public string summry { set; get; }
    }
}


谁能帮我将json对象转换为ICCWorldMatch类型
感谢


Can anyone help me convert json object into ICCWorldMatch type
thanks

推荐答案

也许是这样:

Maybe this:

var l = (ICCWorldMatch)JsonConvert.DeserializeObject(strResult);


尝试以下


try the following


string jSonString = string.Empty;
        WebRequest objRequest = HttpWebRequest.Create(@"http://json-cricket.appspot.com/score.json");
        WebProxy objProxy = new WebProxy("urproxyserveraddress", true);
        objProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        objRequest.Proxy = objProxy;
        WebResponse objResponse = objRequest.GetResponse();
        using (StreamReader reader = new StreamReader(objResponse.GetResponseStream()))
        {
            jSonString = reader.ReadToEnd();
        }
        System.Web.Script.Serialization.JavaScriptSerializer objSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        ICCWorldMatch objOut = (ICCWorldMatch)objSerializer.Deserialize(jSonString, typeof(ICCWorldMatch));



另一个小的更改,请将属性名称更改为 ICCWorldMatch 类的 summary .否则,该属性将保留空值(因为它与JSON结构不匹配)
请让我知道它是否可以解决Ur问题



another small change, please change the property name to summary of the ICCWorldMatch class. Otherwise the property will hold empty value (as it doesn''t matches with the JSON Structure)
please, let me know whether it solves Ur problem


这篇关于asp.net 3.5 Web应用程序中的json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆