Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
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:
C#
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; }
    }
}

Can anyone help me convert json object into ICCWorldMatch type
thanks
Posted
Updated 28-Feb-11 2:39am
v2
Comments
Manfred Rudolf Bihy 28-Feb-11 8:40am    
Edit: Adjusted pre tags.

Maybe this:

C#
var l = (ICCWorldMatch)JsonConvert.DeserializeObject(strResult);
 
Share this answer
 
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));


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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900