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 need to read each value from a json result i request to a controller action. this is my client side.

$.ajax({
        type: "POST",
        url: "List",
        data: { firstArray: arrayCountries, secondArray: arrayLang },
        success: function (result) {

             //here i need each value from json, but it's not working for me   
        },
        dataType: "json",
        traditional: true
    });

Ok now i use this way because i need to send 2 javascript arrays and after finding a lot this way works for me.

And this is my server side. According to How can I post an array of string to ASP.NET MVC Controller without a form? i have created this.

public ActionResult List(List<String> firstArray, List<String> secondArray)
{
    //But here i need a list of sections, so i call a method for that
      var sections = SomeClass.getSections(fistArray, secondArray);

    //And return the json with the collection 
      return Json(sections, JsonRequestBehavior.AllowGet);
}

Now this is my getSections Method

public static IEnumerable getSections(List<String> firstArray, List<String>secondArray)
{
    entities db = new entities();

    var result = from values in db.Sections
                 where ... 
                 select new SectionsModel{ // do something  };

    return result.OrderBy(p => p.Description);
}

Now im getting well json in client side but i don't know how to acces it, and how can i get each value from it ? ... thanks in advance.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You need to parse it first, after that you can just access the properties similarly as what you would do in .NET

$.ajax({
        type: "POST",
        url: "List",
        data: { firstArray: arrayCountries, secondArray: arrayLang },
        success: function (result) {

             var sections = JSON.parse(result);

             // access properties here
        },
        dataType: "json",
        traditional: true
    });
share|improve this answer
    
im getting firebug JSON.parse: unexpected character ... is something wrong with json format ? –  Steve Apr 23 '13 at 23:00
    
What is the value of result when the call completes? –  Kenneth Apr 23 '13 at 23:01
    
it's something like [object Object],[object Object] –  Steve Apr 23 '13 at 23:06
    
Then you should be able to access your items already –  Kenneth Apr 23 '13 at 23:07
    
thanks for your help finally with result[0].property i can get values. :) –  Steve Apr 23 '13 at 23:18

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.