Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all
i have json string like this:
{name:\"Alex\", age:\"34\"}
how to deserialize a that string in c#.when ever i click the button the the deserialized string will be displayed in label like: name:alex,age:34---this is output iwant..
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using System.Runtime.Serialization;
using System.IO;



namespace deserializestring
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
public class myobject
       {
           public string name { get; set; }
           public string age { get; set; }
       }
private void button1_Click(object sender, EventArgs e)
      {
string json="{name:\"Alex\", age:\"34\"}"
JavaScriptSerializer jss=new JavaScriptSerializer();
myobject obj=jss.Deserialize<myobject>(json);
label1.text=obj;


but its not coming... pls let me know the solution .....
and do the nedd full...

[edit]< and > HTML encoded, spare closing tags removed[/edit]
Posted
Updated 13-Jul-11 0:10am
v3

1 solution

You seem to be asking this a lot of times, but this time you might have given us enough information. Have you tried overiding ToString in your myobject class:
    string json = "{name:\"Alex\", age:\"34\"}";
    JavaScriptSerializer jss = new JavaScriptSerializer();
    MyObject obj = jss.Deserialize<MyObject>(json);
    myLabel.Text = obj.ToString();
    ...

private class MyObject
    {
    public string name { get; set; }
    public string age { get; set; }
    public override string ToString()
        {
        return string.Format("name:{0}, age:{1}", name, age);
        }
    }
 
Share this answer
 

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