2

I already have these structure of class object in my web service develop by another team. I need to post JSON data to class CartObject (int CustomerID, List<CartListObject> CartList).

Here the elements inside CartListObject :

public int ItemID { set; get; }
public ItemObject Item { set; get; }
public int Quantity { set; get; }

ItemObject :

public int ID { set; get; }
public string Name { set; get; }
public List<ItemImageObject> ItemGallery { set; get; }

ItemImageObject :

public int ItemID { set; get; }
public string ImageName { set; get; }

I have no idea how is the JSON data of CartObject that I should post to web service.

Any idea is appreciated.

0

2 Answers 2

1

jsFiddle Demo

Basically you are going to need to set up constructors to make these objects in javascript. The naming must match exactly to the expected type so that the data will automatically bind properly.

This means copying almost an exact setup of the model in javascript:

var CartListObject = function(itemId,item,quantity){
 this.ItemId = itemId;
 this.Item = item;//ItemObject
 this.Quantity = quantity;
};

var ItemObject = function(id, name, itemGallery){
 this.ID = id;
 this.Name = name;
 this.ItemGallery = itemGallery;//List<ItemImageObject>
};

var ItemImageObject = function(itemId, imageName){
 this.ItemID = itemId;
 this.ImageName = imageName; 
};

And then building it with your data like this (fake data for example):

var CartList = [];//List<CartListObject>

var someItem = new ItemImageObject(1,"hello");
var someItem2 = new ItemImageObject(2,"world");

var someObj = new ItemObject(1,"holder",[someItem,someItem2]);

var someCart = new CartListObject(1,someObj,4);

CartList.push(someCart);

And then using it in your ajax post (or normal post)

//post ajax:
//data: { CustomerID: 6, CartList: CartList },
Sign up to request clarification or add additional context in comments.

Comments

1

You're question is vague regarding the receiving end: What language/framework/libs are receiving the JSON?

Also, the receiving application will need to be type-aware and cast appropriately. JSON consists of primitive values (Object, Array, Number, String), so objects like ItemObject, ItemImageObject will be lost. JSON is also not aware of List strict-typed vectors - but these would translate to arrays.

I'm sure there are more ways to do this, but this would be my take (with some bogus values):

{
    "CustomerID": 102,
    "CartList": [
        {
            "ItemID": 1,
            "Item": {
                "ID": 4242,
                "Name": "Lorem Ipsum",
                "ItemGallery": [
                    {
                        "ItemID": 55,
                        "ImageName": "Dolor Sit"
                    },
                    {
                        "ItemID": 56,
                        "ImageName": "Amet Palor"
                    }
                ]
            },
            "Quantity": 12
        },
        {
            "ItemID": 2,
            "Item": {
                "ID": 5656,
                "Name": "Edipiscing Elit",
                "ItemGallery": [
                    {
                        "ItemID": 62,
                        "ImageName": "Tellus Eros"
                    },
                    {
                        "ItemID": 63,
                        "ImageName": "Velit Nec"
                    }
                ]
            },
            "Quantity": 13
        }
    ]
}

9 Comments

I post this JSON data to restful web service develop using c#. One more thing, the JSON that I want to send to REST also include CustomerID of CartObject.
I've tried in here http://jsfiddle.net/N3BNw/ but when I tested in chrome POSTMAN, I got an error Request Error The server encountered an error processing the request. See server logs for more details..
@Domo remove the encapsulating array notation, see jsfiddle.net/N3BNw/1; also i've truncated the item description for the purpose of example - javascript doesn't handle multi-line string literals (although as a value it does just fine). Also you have a lot more properties than what is in the question, i'm sure you left them out for simplicity's sake? Travis J's answer might make more sense, just gotta JSON.stringify the CartList (actually update it to handle the CustomerID and CartObject). Oh and I updated my answer.
I tried your solution dear, but I'm still getting the same error.
@Domo perhaps the server requires more than just that JSON (or more properties); ask the other team for an API specification. What method/lib/procedure are you using to send the data to the service (however this is beyond the scope of the original question)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.