none
JSON & W phone 7

Answers

  • Yes there is you can use the DataContractJsonSerializer

    Make sure you reference the appropriate assembly: System.Servicemodel.Web

    Thursday, March 18, 2010 1:20 AM
  • You should read up on this or try google next time.  There are many examples.  However, here is a sample I wrote taking a simple Json integer array and serializing it into an integer list:

    string jsonString = "[ 1, 2, 3, 4, 5, 6 ]";

    List<int> listArray = new List<int>();

    using (MemoryStream jsonStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))

    {

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<int>));List<int> array = serializer.ReadObject(jsonStream) as List<int>;

    listArray = array;

    }

    Thursday, March 18, 2010 11:56 AM
  • Create a class that has encompasses this.  Then you can serialize it that object.

    In your example you create a class called Student with properties Name, ID etc. and then in my example change the List<int> into List<Student> everywhere.

    What you are asking is not specific to the Windows Phone 7...this is standard Json deserialization practices.

    Thursday, March 18, 2010 3:22 PM
  • As you pointed out the JSON string is simply incorrect.  If you had the courses nested inside the object then that would be fine and you could just deserialize a List<student> as the courses would be nested.

    You will NOT be able to use standard JSON deserialization classes as it your JSON string is incorrect.  If that is the way you get it from the server and cannot change it unfortunately you will have to write a CUSTOM string parser.

    What I would do is use a custom split on "}," characters and that will split the student and course objects.  From there you can loop through each object and if it starts from "name" pass that object to the JSON serializer for Student.  If its a course pass it for the course.  You will manually have to build the main list up.

    So I hope this works for you and gets you on your way.  If you are still having problems with it...I can help you with the code.  Its going to be hacky and non-standard, but your JSON is what it is :)

     

    Friday, March 19, 2010 2:48 PM

All replies

  • Yes there is you can use the DataContractJsonSerializer

    Make sure you reference the appropriate assembly: System.Servicemodel.Web

    Thursday, March 18, 2010 1:20 AM
  • Thanks, but how I can read an array !? As there is a very limited methods like : ReadObject(Stream) But nothing like ReadArray(,, !? Thanks, for the help really appreciate it
    Thursday, March 18, 2010 5:19 AM
  • Double post..removing

    Thursday, March 18, 2010 11:55 AM
  • You should read up on this or try google next time.  There are many examples.  However, here is a sample I wrote taking a simple Json integer array and serializing it into an integer list:

    string jsonString = "[ 1, 2, 3, 4, 5, 6 ]";

    List<int> listArray = new List<int>();

    using (MemoryStream jsonStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))

    {

    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<int>));List<int> array = serializer.ReadObject(jsonStream) as List<int>;

    listArray = array;

    }

    Thursday, March 18, 2010 11:56 AM
  • OK, this is clear but what if there is a a different objects in one array also there is an other object before the array !? For example : We have 3 class : SomeData, Student, City JSONString = "{"requestNum"=5,"data"="somedata"},[{"Name"="student1","ID"=65435},{"Name"="student2","ID"=6545},{city="somewhare","Whather"="hot"}]" Here you can see in the JSON string there is : Object(SomeData), array(Object(student),Object(student),Object(City)) so there is no one type of object or attribute I can tell the DataContractJsonSerializer to read there is a mixed objects !! Please help, and I'm still searching and didn't find my solution :(
    Thursday, March 18, 2010 3:00 PM
  • Create a class that has encompasses this.  Then you can serialize it that object.

    In your example you create a class called Student with properties Name, ID etc. and then in my example change the List<int> into List<Student> everywhere.

    What you are asking is not specific to the Windows Phone 7...this is standard Json deserialization practices.

    Thursday, March 18, 2010 3:22 PM
  • OK, I'll try it,,,,, thanks for the big help ^_^
    Thursday, March 18, 2010 3:31 PM
  • Hi, When I tried to write what you said, it doesn't fit my problem as I have different object in the beginning !!

    For example:

     * Every student has a list of courses.

     * The JSON string contain an array.

     * The first object in the array is the student info.

     * The rest of the objects is the courses that student have.

     * So it's should be like this :

      

    [{"name":"student","ID":123456,"NoOfCourse":3},{"name":"course1","title":"title1"},{"name":"course2","title":"title2"},{"name":"course3","title":"title3"},..]

     * Here is the student & course class : 

    public class student{ 
    string name;
     int ID;
     int NoOfcourse;
     List<course> courses;
     } 
    
    public class course{
     string name;
     string title;
     }
    

     I know it's looks wrong to put the the student object in the same courses array put this how I receive it from the server and I don't have control to change that :(

     --> What I need, is to parse all the JSON string in an object of student class which has the student info and a list of courses.

     Waiting for your answer, I really have hard time to solve this,,it's different then the other JSON library,,, I think it's easy and smart one but I need to know how to work with it first ;),,,,,, Thanks

    Friday, March 19, 2010 2:15 PM
  • As you pointed out the JSON string is simply incorrect.  If you had the courses nested inside the object then that would be fine and you could just deserialize a List<student> as the courses would be nested.

    You will NOT be able to use standard JSON deserialization classes as it your JSON string is incorrect.  If that is the way you get it from the server and cannot change it unfortunately you will have to write a CUSTOM string parser.

    What I would do is use a custom split on "}," characters and that will split the student and course objects.  From there you can loop through each object and if it starts from "name" pass that object to the JSON serializer for Student.  If its a course pass it for the course.  You will manually have to build the main list up.

    So I hope this works for you and gets you on your way.  If you are still having problems with it...I can help you with the code.  Its going to be hacky and non-standard, but your JSON is what it is :)

     

    Friday, March 19, 2010 2:48 PM
  • Thanks for the fast response.

    I didn't get your idea of putting "}," !!? can you add it in this example :

    [{"name":"student","ID":123456,"NoOfCourse":3},{"name":"course1","title":"title1"},{"name":"course2","title":"title2"},{"name":"course3","title":"title3"},..]

    I have another idea is to modify the string after receiving it, so it's will be like this :

    [{"name":"student","ID":123456,"NoOfCourse":3},"courses":[{"name":"course1","title":"title1"},{"name":"course2","title":"title2"},{"name":"course3","title":"title3"},..]}

    So the final result will be like this :

    {"name":"student","ID":123456,"NoOfCourse":3,"coerses":[{"name":"course1","title":"title1"},{"name":"course2","title":"title2"},{"name":"course3","title":"title3"},..]}

    What do you think ?

    Friday, March 19, 2010 3:33 PM
  • Folks I could not find the DataContractJsonSerializer in the Windows Phone 7 SDK.. Am I missing something ?

    Wednesday, September 08, 2010 5:23 PM
  • You need to add a reference to the System.ServiceModel.Web.dll assembly to use the DataContractJsonSerializer class.

    See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer(VS.95).aspx

    Monday, September 20, 2010 11:39 AM
  • hello, i'm new with silverlight and phone 7.

    i have a question about consuming data with json.

    my example is:

    {"result":
        { "students":
            [ {"name":"s1","ID":1},{"name":"s2","ID":2} ]
        }
    }


    i don't succes to map the class with Silverlight for Phone 7

    [DataContract]
    public Class contenitor {
    
        [DataMember]
        public resultClass result {get; set;}
    }
    
    
    [DataContract]
    public Class resultClass {
    
        [DataMember]
        public List<studentClass> students {get; set;}
    }
    
    
    [DataContract]
    public Class studentClass {
    
    
        [DataMember]
        string name { get; set; }
    
        [DataMember]
        int ID  {get; set; }
    }


    maybe the solution is simple but i don't find it, if you need more information i will give you.

    EDIT: Sorry, the solution that i wrote above works good

    thank
    Domenico

    Friday, November 26, 2010 6:31 AM
  • Hello Guys,

    I am new to windows phone7. In my app we are using webervices. For one request if there is data , it appears as

    {"login_status":"1","req_start_time":"2011-12-15 19:19:01","req_end_time":"2011-12-15 19:19:01","exec_tine":"Page Execution Takes : 0.08275580406189 seconds","user_details":{"user_id":"m6uroJqomp-Ylag","fullname":"deepthi","dispic":"T_pic_O21308644187_new.jpeg","gender":"F","username":"talluri","id":"2","user_pic":"http:\/\/userphotos.mojostreet.com.s3.amazonaws.com\/2\/T_pic_O21308644187_new.jpeg","show_invites":0},"r_place":{"place_id":"21451","name":"Xtreme Sports Bar","longitude":"78.449767","latitude":"17.415200"},"sn_linkups":{"fb_linked":1,"fb_share":"Y","linkedin_share":"Y","twit_share":"Y","ir_share":"Y","ms_share":"1"},"new_version":0,"new_version_msg":"","user_stats":{"tips_cnt":"3","checkin_cnt":"9","mojo_dollars":"153","traveled":14,"photos_cnt":"2","property_cnt":"3"},"f_feeds":[{"user_id":"3","place_id":"7321","checkin_id":"2087","message":"","dis_timestamp":"2 day(s) ago","place_name":"Ofen","address":"Banjara Hills","city":"Hyderabad","fullname":"bharath","user_pic":"http:\/\/userphotos.mojostreet.com.s3.amazonaws.com\/3\/T_pic_pic_Cbq1323415433.jpeg","gender":"M"},{"user_id":"244","place_id":"25491","checkin_id":"1595","message":"home sweet home........","dis_timestamp":"2 month(s) ago","place_name":"Honey Villa","address":"Plot. No.107 Mithila Nagar","city":"Hyderabad","fullname":"karthik","user_pic":"http:\/\/userphotos.mojostreet.com.s3.amazonaws.com\/244\/T_pic_pic_PrJ1315300411.jpeg","gender":"M"},{"user_id":"97","place_id":"25386","checkin_id":"1506","message":"enjoying the sunday brunch","dis_timestamp":"2 month(s) ago","fullname":"Anju Jak","user_pic":"http:\/\/userphotos.mojostreet.com.s3.amazonaws.com\/97\/T_pic_pic_zeB1315643440.jpeg","gender":"F"}],"feeds_cnt":"5","f_requests":"0","ex_info":[{"cat_id":23,"cat_name":"Information Technology Centers","cat_img":"c23"},{"cat_id":24,"cat_name":"Offices","cat_img":"c24"},{"cat_id":25,"cat_name":"Park or Recreation Center","cat_img":"c25"},{"cat_id":26,"cat_name":"Petrol\/Gasoline Station","cat_img":"c26"},{"cat_id":27,"cat_name":"Pharmacy","cat_img":"c27"},{"cat_id":28,"cat_name":"Police Station","cat_img":"c28"}]}

    If user has no place data r_place comes as empty array. Below is how it looks..

    {"login_status":"1","req_start_time":"2011-12-15 19:19:52","req_end_time":"2011-12-15 19:19:52","exec_tine":"Page Execution Takes : 0.067158222198486 seconds","user_details":{"user_id":"m6uroJqomp-dlq-ekw","fullname":"wind","dispic":null,"gender":"F","username":"","id":"980","user_pic":"np_female.png","show_invites":0},"r_place":[],"sn_linkups":{"ms_share":1},"new_version":0,"new_version_msg":"","user_stats":{"tips_cnt":"0","checkin_cnt":"0","mojo_dollars":"500","traveled":0,"photos_cnt":"0","property_cnt":"0"},"f_feeds":[],"feeds_cnt":0,"f_requests":"0","ex_info":[{"cat_id":23,"cat_name":"Information Technology Centers","cat_img":"c23"},{"cat_id":24,"cat_name":"Offices","cat_img":"c24"},{"cat_id":25,"cat_name":"Park or Recreation Center","cat_img":"c25"},{"cat_id":26,"cat_name":"Petrol\/Gasoline Station","cat_img":"c26"},{"cat_id":27,"cat_name":"Pharmacy","cat_img":"c27"},{"cat_id":28,"cat_name":"Police Station","cat_img":"c28"}]}

    Here is my code for parsing

    userinfo deserializedUser = new userinfo();
    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonResponse));
    DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());
    deserializedUser = ser.ReadObject(ms) as userinfo;

    This is my model class

    public class userinfo
        {
            public int login_status;
            public int recent_checkin_exist;
            public user_details user_details { get; set; }        
            public recent_place r_place { get; set; }        
            public user_stats user_stats { get; set; }
            public social_network_linkups sn_linkups { get; set; }
            public int new_version;
            public string new_version_msg { get; set; }
        }
        public class user_details
        {
            public string username { get; set; }
            public string password { get; set; }
            public string user_id { get; set; } //user authentication code
            public int id { get; set; } //original user id
            public string fullname { get; set; }
            public string user_pic { get; set; }
            public string dispic { get; set; }
            public string gender { get; set; }
            public int show_invites { get; set; }
        }    
        public class recent_place
        {  
            public string place_id { get; set; }
            public string name { get; set; }
            public string longitude { get; set; }
            public string latitude { get; set; }
        }
        public class social_network_linkups
        {
            public int fb_linked { get; set; }
            public string fb_share { get; set; }
            public string linkedin_share { get; set; }
            public string twit_share { get; set; }       
            public string ms_share { get; set; }
            public string ir_share { get; set; }
        }
        public class user_stats
        {
            public string tips_cnt { get; set; }
            public string checkin_cnt { get; set; }
            public string mojo_dollars { get; set; }
            public string traveled { get; set; }
            public string photos_cnt { get; set; }
            public string property_cnt { get; set; }

        }

    While running the app i am getting invalid cast exception. How can i solve this problem I tried in different ways, but i didnt get it done.

    Waiting for reply..

    Thanks,

    talluri

    Thursday, December 15, 2011 8:56 AM