How to convert Json String to Object with Dictionary using JayRock

786 views
Skip to first unread message

张涛 黄

unread,
May 29, 2012, 11:43:38 AM5/29/12
to Jayrock
Hello everyone, I'm new to jayrock, also C# :)
I want to convert a Json string to an Object in C#. the string is like
this:
{"dealName":"name1","properties":{"a":"1", "b":"2"}}

The class defined like this:
public class DealInfo
{
public string dealName;
public Dictionary<string, string> properties;
}

And I use this code to convert:
DealInfo dl = JsonConvert.Import(typeof(DealInfo), jsonString) as
DealInfo;

I found it just converted the dealName field, but the properties field
count is 0.
So, what's the problem? How to fix it?
Many thanks!

Atif Aziz

unread,
May 29, 2012, 12:26:25 PM5/29/12
to jay...@googlegroups.com
Try with Dictionary changed to IDictionary, like this:
 
public class DealInfo {
  public string dealName;
  public IDictionary<string, string> properties;
}
- Atif

--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To post to this group, send email to jay...@googlegroups.com.
To unsubscribe from this group, send email to jayrock+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jayrock?hl=en.


张涛 黄

unread,
May 30, 2012, 1:56:41 AM5/30/12
to Jayrock
It still doesn't work.
when using import method, it throws JsonException "don't known how to
import System.Collections.Generic.IDictionary'2..."
I'm using the .net 1.1 releaed package.

黄张涛

unread,
May 30, 2012, 2:19:45 AM5/30/12
to Jayrock
The full exception message is:
Don't know how to import System.Collections.Generic.IDictionary'2
[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089],[System.String, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] from JSON


2012/5/30 张涛 黄 <huang...@gmail.com>

Atif Aziz

unread,
May 30, 2012, 2:53:53 AM5/30/12
to jay...@googlegroups.com
If you're using generic types, why aren't you using the binaries for .NET 2.0?

- Atif

黄张涛

unread,
May 30, 2012, 3:23:13 AM5/30/12
to jay...@googlegroups.com
we are using a very old system developed centuries ago, and developing a plugin-in for it, and we can't change the run-time environment.
Can the work be done with the Jayrock? 
And now I'm thinking of manual conversion form JsonObject to my object. 
 

2012/5/30 Atif Aziz <aziz...@gmail.com>

Atif Aziz

unread,
May 30, 2012, 3:37:07 AM5/30/12
to jay...@googlegroups.com
If the runtime environment is 1.1 then I'm surprised you were able to load your own assembly at all considering it uses generics like Dictionary<string, string>. Another option you have is to simple type your properties property as non-generic IDictionary from System.Collections namespace.
 
- Atif

张涛 黄

unread,
May 30, 2012, 7:19:46 AM5/30/12
to Jayrock
Thank you very much.
I studied generic & non-generic type in .Net Framework, and finally
solved the problem, thank you again!


On 5月30日, 下午3时37分, Atif Aziz <aziza...@gmail.com> wrote:
> If the runtime environment is 1.1 then I'm surprised you were able to load
> your own assembly at all considering it uses generics like
> Dictionary<string, string>. Another option you have is to simple type your
> properties property as non-generic IDictionary from System.Collections
> namespace.
>
> - Atif
>
>
>
>
>
>
>
> On Wed, May 30, 2012 at 9:23 AM, 黄张涛 <huangzh...@gmail.com> wrote:
> > we are using a very old system developed centuries ago, and developing a
> > plugin-in for it, and we can't change the run-time environment.
> > Can the work be done with the Jayrock?
> > And now I'm thinking of manual conversion form JsonObject to my object.
>
> > 2012/5/30 Atif Aziz <aziza...@gmail.com>
>
> >> If you're using generic types, why aren't you using the binaries for .NET
> >> 2.0?
>
> >> - Atif
>
Reply all
Reply to author
Forward
0 new messages