1

i am converting a vb project to c#;
everything is ok except this line:

information = DirectCast(jsonserializer.Deserialize(jsonString, GetType(List(Of formData))), List(Of formData))

i created a class(formData) just same as vb, and json string is the same as vb.

    public class formData
{
    private string part;
    private string key;
    private string val;
    public formData(string part, string key, string val)
    {
        this.part = part;
        this.key = key;
        this.val = val;
    }

    public string Part
    {
        get { return part; }
        set { part = value; }
    }

    public string Key
    {
        get { return key; }
        set { key = value; }
    }

    public string Value
    {
        get { return val; }
        set { val = value; }
    }
}

what should i do?
i have to use "http://json.codeplex.com/"?

mohammad adibi
  • 115
  • 1
  • 8
  • DirectCast can be translated to C# as a cast. That is: DirectCast(myOjbect, myType) in VB.NET equals (myType)myObject in C# – varocarbas Oct 31 '13 at 14:35

1 Answers1

1

Directcast in VB is the same as casting to type in C#.

So, I believe your code would look something like this:

var jsonserializer = new JavaScriptSerializer();
var information = jsonserializer.Deserialize(jsonString, typeof(List<formData>)) as List<formData>;

or

var jsonserializer = new JavaScriptSerializer();
var information = (List<formData>)jsonserializer.Deserialize(jsonString, typeof(List<formData>));

The difference between the two is that the former will return null if the cast fails whereas the latter will throw an exception. There is a great discussion between casting as type and as on SO here:

Difference between type cast and 'as' cast

Community
  • 1
  • 1
crthompson
  • 15,653
  • 6
  • 58
  • 80
  • 1
    It should be something like `information = (List)jsonserializer.Deserialize(jsonString, typeof(List));` – sloth Oct 31 '13 at 14:40
  • Also the cast should never fail as the deserializer knows what type to deserialize to (I hope this sentence makes sense :-) – sloth Oct 31 '13 at 14:41
  • You have still to convert formData into List You are casting to formData and the original code casts to List – varocarbas Oct 31 '13 at 14:45
  • 1
    I did the editing myself to avoid misunderstandings (hope you don't mind but was a bug anyway so...) – varocarbas Oct 31 '13 at 14:51
  • thanks all: Erro The name 'jsonserializer' does not exist in the current context, i use : using System.Web.Script.Serialization; but i get this error. please help. – mohammad adibi Oct 31 '13 at 15:08
  • @mohammadadibi how are you declaring `jsonserializer` in VB? – crthompson Oct 31 '13 at 15:11
  • Imports System.Web.Script.Serialization – mohammad adibi Oct 31 '13 at 15:18
  • [System.Web.Script.Serialization](http://msdn.microsoft.com/en-us/library/system.web.script.serialization.aspx) doesnt have a property called jsonserializer. This is a variable that you have declared. How or where did you declare it in VB? – crthompson Oct 31 '13 at 15:43
  • Just based on your comments i'm guessing that you want `JavaScriptSerializer`, i've edited the post. – crthompson Oct 31 '13 at 15:56
  • yes you are right, that problem solved, but it doesn't work yet, let me check and inform you. thanks. – mohammad adibi Oct 31 '13 at 15:59
  • jsonString is a list of object, now i give this error: No parameterless constructor defined for type of 'ClearWhite.Classes.formData', would you helpme? – mohammad adibi Oct 31 '13 at 16:11
  • The constructor is your method called formData as you've listed above. Notice that it accepts parameters. A parameterless constructor is one that accepts no parameters. `public formData(){//do something here?}` – crthompson Oct 31 '13 at 16:16
  • Check out [this SO question](http://stackoverflow.com/questions/9386929/no-parameterless-constructor-defined-for-type-of-system-string-during-json-des) – crthompson Oct 31 '13 at 16:19
  • "[{\"part\":\"head\",\"key\":\"province\",\"val\":\"0\"},{\"part\":\"head\",\"key\":\"city\",\"val\":\"\"},{\"part\":\"head\",\"key\":\"selectedTab\",\"val\":\"1\"},{\"part\":\"controls\",\"key\":\"SELECT\",\"val\":\"1\"},{\"part\":\"controls\",\"key\":\"SELECT\",\"val\":\"4\"},{\"part\":\"controls\",\"key\":\"SELECT\",\"val\":\"10\"},{\"part\":\"controls\",\"key\":\"checkbox\",\"val\":\"18\"},{\"part\":\"controls\",\"key\":\"radio\",\"val\":\"22\"},{\"part\":\"controls\",\"key\":\"text\",\"val\":\"b b\"}]" – mohammad adibi Oct 31 '13 at 16:21
  • This is not a great forum for posting code. Perhaps you should accept this answer and post another question. – crthompson Oct 31 '13 at 16:22
  • above is my json and in my question i wrote class, i can't catch the error! – mohammad adibi Oct 31 '13 at 16:22
  • this is what i forgot to declare: JavaScriptSerializer jsonserializer = new JavaScriptSerializer(); everything must be ok. – mohammad adibi Oct 31 '13 at 16:23