Search code examples
javascriptjqueryjsonrazorhtml-helper

How to parse a jsonarray stored in a viewBag using jquery


I am not able to parse the jsonArray in the viewBag. I want to get the contents and then use them add options to a

Upon using a console.log I could see that my json is as follows :

So far I have an array with a single element

[  
   {  
      "BackgroundColor":null,
      "BaseFormat":"PNG",
      "ColorFormat":"RGB",
      "ColorProfile":null,
      "Density":"300",
      "Extra_param":null,
      "FileFormat":"JPG",
      "PresetId":"2",
      "Resolution":"",
      "Quality":100
   }
]

I have the contents in a variable as below:

var presetArray = @Html.Raw(Json.Encode(@ViewBag.user_doc_presets)); console.log(presetArray);

I want to get the BaseFormat and colorformat to be used into a select.

I tried some stackoverflow posts but they didn't help me.

If you have a link to an old post or a hint please do share. @downvoters, Please let me know if you have any questions regarding this post instead of downvoting covertly.


Solution

  • I was missing a Json.Parse, which solved the problem

    Please refer to http://jsfiddle.net/jresdqw3/

    var arr = [  
       {  
          "BackgroundColor":null,
          "BaseFormat":"PNG",
          "ColorFormat":"RGB",
          "ColorProfile":null,
          "Density":"300",
          "Extra_param":null,
          "FileFormat":"JPG",
          "PresetId":"2",
          "Resolution":"",
          "Quality":100
       }
    ];
    
    alert(arr.length);
    alert(JSON.stringify(arr).length);