IList from jsonArray

661 views
Skip to first unread message

FruitBatInShades

unread,
Oct 6, 2009, 11:31:14 AM10/6/09
to Jayrock
I am trying to handle a jsonArray and convert it to an IList. The
json is

{[
{
"ca":"left",
"isContent":false,
"id":"4",
"position":0
},{
"ca":"content",
"isContent":false,
"id":"3",
"position":0
}
]}

the c# object is :

public class Placement
{
public string ca;
public bool isContent;
public int position;
public int id;
}

and I'm trying to parse them as follows:

public string SetPlacement(JsonArray parts)
{
string s = "";
IList Parts = (IList)JsonConvert.Import(typeof(IList),
parts.ToString());
foreach (Placement p in Parts) {
s += p.ca;
}
return "success";
}
I can't figure out how to get the IList to be populated with built
Placement objects. I'm guessing the need to be parsed as well. Can
anyone help me?

Atif Aziz

unread,
Oct 6, 2009, 4:11:09 PM10/6/09
to jay...@googlegroups.com
Instead of this:
 
(IList)JsonConvert.Import(typeof(IList),parts.ToString());
 
Try this:
 
(Placement[])JsonConvert.Import(typeof(Placement[]),parts.ToString());
 
If it works and you're interested on why, I can expand further.
 
- Atif

FruitBatInShades

unread,
Oct 7, 2009, 4:54:31 AM10/7/09
to Jayrock
Hi Atif, I'd ;ove to get a better understanding of jayrock.

That didn't work. It seems the placement object can't be recreated. Do
I need to add an attribute or structure it in a certain way?
====================================================================================================
Err: SaveBlock Failed with Don't know how to import Placement from
JSON.
====================================================================================================
public class PlacementHandler : JsonRpcHandler,
System.Web.SessionState.IRequiresSessionState
{
[JsonRpcMethod("setPlacement", Idempotent = true)]
[JsonRpcHelp("Save the current locations to the db.")]
public string SetPlacement(JsonArray parts)
{
try {
string s = "";
IList Parts = (Placement[])JsonConvert.Import(typeof
(Placement[]), parts.ToString());
//for (int i = 0; i < parts.Length;i++ ) {
// Placement p = (Placement)JsonConvert.Import(typeof
(Placement), parts[i].ToString());
//}
return "success";
} catch (Exception ex) {
fbisPersonalCms.Utils.JsonError err = new
fbisPersonalCms.Utils.JsonError("SaveBlock Failed with " + ex.Message,
0);
return JsonConvert.ExportToString(err);
//throw;
}
}
}
public class Placement
{
public Placement(string ca, bool isContent, int position, int id)
{
this.ca = ca;
this.isContent = isContent;
this.position = position;
this.id = id;
}
public string ca;
public bool isContent;
public int position;
public int id;

}

On Oct 6, 9:11 pm, Atif Aziz <aziza...@gmail.com> wrote:
> Instead of this:
>
> > (IList)JsonConvert.Import(typeof(IList),parts.ToString());
>
> Try this:
>
> > (Placement[])JsonConvert.Import(typeof(Placement[]),parts.ToString());
>
> If it works and you're interested on why, I can expand further.
>
> - Atif
>
> On Tue, Oct 6, 2009 at 5:31 PM, FruitBatInShades <fruitbatinsha...@gmail.com

FruitBatInShades

unread,
Oct 7, 2009, 5:10:59 AM10/7/09
to Jayrock
Just added public placement(){} and it works :) Still interested in
knowing more please :)
I'm assuming its using reflection to iterate the properties on the
target class and then converting the json item to the correct type?

Atif Aziz

unread,
Oct 7, 2009, 5:38:05 PM10/7/09
to jay...@googlegroups.com
I'm assuming its using reflection to iterate the properties on the
target class and then converting the json item to the correct type

Yes.

The reason IList did not work is that IList is weakly typed in terms of System.Object (the widest type) items whereas you wanted a list of a sepcific item type (Placement). When you tell Jayrock to import as Placement[], Jayrock has more information. It knows you want an array but also that the array is to contain Placement objects. With this, it can start importing the JSON array and then each item of the JSON array as a Placement object in C#. With IList, Jayrock is still able to import, which is why it succeeds. However, Jayrock will import each JSON value it encounters into a suitable object based as described here:
  • A JSON array will be imported as JsonArray
  • A JSON object will be imported as JsonObject
  • A JSON number will be imported as JsonNumber
  • A JSON string will be imported as a C# string (i.e. System.String)
  • A JSON Boolean will be imported as a C# bool (i.e. System.Boolean)
  • A JSON null will become a null reference in C#
Importing IList is like importing object[]. There's no real difference except the latter is fixed size.
 
Hope this helps,
Atif

Reply all
Reply to author
Forward
0 new messages