Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am creating json string my javascript and sending it to my controller in mvc application. my code which create json string :

     $('#btnassign').click(function () {

            var arrPrice = "";
            var arrMarkUP = "";

            $("table tr").each(function () {

                if ($(this).find('input:checkbox:first').is(':checked')) {

                    if ($(this).find('input.inputprice').val() != "") {
                        arrPrice += "{";
                        var price = $(this).find('input.inputprice').val();
                        var id = $(this).find('input[type=hidden]').val();
                        arrPrice += '"Id":"' + id + '","Price":"' + price + '"';
                        arrPrice += "},";
                    }
                    if ($(this).find('input.inputmarkup').val() != "") {
                        arrMarkUP += "{";
                        var price = $(this).find('input.inputmarkup').val();
                        var id = $(this).find('input[type=hidden]').val();
                        arrMarkUP += "Id:" + id + ",Price:" + price;
                        arrMarkUP += "},";
                    }
                }
            });

            var lastindexp = arrPrice.lastIndexOf(",");
            arrPrice = arrPrice.substring(0, lastindexp) + "|";
            var lastindexm = arrMarkUP.lastIndexOf(",");
            arrMarkUP = arrMarkUP.substring(0, lastindexm) + "|";
            alert(arrPrice);
            alert(arrMarkUP);

            $("#hdPrice").val(arrPrice);
            $("#hdMarkUP").val(arrMarkUP);

            $("#AssignProductForm").submit();
        });

    });

my json string generates from above code :

    {"Id":"1","Price":"4"},{"Id":"2","Price":"6"}

My Controller parsing code :

    [HttpPost]
    public ActionResult AddProducts(FormCollection collection, string txtsearch)
    {
        var ManualPricing = collection["hdPrice"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

         JavaScriptSerializer ser = new JavaScriptSerializer();
         var Manual = ser.Deserialize<PriceMargin>(ManualPricing[0]);         
    }

price margin is a class to which it should deserialize :

    public class PriceMargin
    {
        public string Id { get; set; }
        public string Price { get; set; }
    }

line var Manual = ser.Deserialize(ManualPricing[0]); gives me an error :Invalid JSON primitive: {"Id":"2","Price":"9"}.

please help ASAP.

Solution :

var list = new JavaScriptSerializer().Deserialize<List<KeyValue>>(json);

public class KeyValue
{
   public string key;
   public string value;
}
share|improve this question
3  
Why are you manually building the string? You can use native datatypes and then encode those datatypes to JSON strong and decode the JSON string to native datatypes –  dm03514 Jul 7 '13 at 16:34
    
i don't know how to do it , i am new to javascript , jquery !! any example ? –  DharaPPatel Jul 7 '13 at 16:36
    
added example on using javascript objects –  dm03514 Jul 7 '13 at 16:41
add comment

3 Answers

Your JSON is invalid.

JSON can only have a single root object.

If you want to have multiple objects, serialize an array.

share|improve this answer
    
means ? what my JSON should be in this context ??? –  DharaPPatel Jul 7 '13 at 16:38
add comment

To follow up the comment, if you want an array of objects to you could do something like:

pricesArray = [];
$("table tr").each(function () {
  if ($(this).find('input:checkbox:first').is(':checked')) {
    var price = $(this).find('input.inputprice').val();
    var id = $(this).find('input[type=hidden]').val();
    pricesArray.push({Id : id, Price: price});
  }
});
// pricesArray [{"Id":"1","Price":"4"},{"Id":"2","Price":"6"}]

pricesArray should now contain the data you want, it should be much easier to serialze and desearilze your data and send it around now

share|improve this answer
add comment

Your JSON would be much easier like that:

[["Id":"1","Price":"4"],["Id":"2","Price":"6"]]

as @SLaks said.

share|improve this answer
    
Consider flag this answer if it serves you. –  JohnnyJS Jul 7 '13 at 16:40
1  
This is not JSON –  Musa Jul 7 '13 at 16:42
    
I believe your example object is just as invalid as the OP's –  dm03514 Jul 7 '13 at 16:43
    
JSON is decoding perfectly in the engine at the backend. thats not an object i know. but thats a two dimensional array, that can get decoded and give all the info. –  JohnnyJS Jul 7 '13 at 16:46
    
@JohnnyJS: Wrong; that is invalid syntax. –  SLaks Jul 7 '13 at 18:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.