-1

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.

10
  • console.log(presetArray[0].BaseFormat); ...did you try that? Commented Nov 16, 2018 at 14:29
  • But TBH if you want to put them into a select, why not just create and populate the select directly in MVC/Razor code? This seems a slightly convoluted method. Commented Nov 16, 2018 at 14:30
  • @ADyson, i did try presetArray.length, which gave me a whopping 184 and that's why i got more lost. I will try some more Commented Nov 16, 2018 at 14:32
  • that might be because it's still a string....184 is the number of characters that JSON contains (see jsfiddle.net/jresdqw3). You should be able to get MVC to output it as a literal immediately. failing that, use JSON.parse() in the JS to turn it into an array before trying to use it. Commented Nov 16, 2018 at 14:35
  • 1
    @ADyson, you are right, i can do it from the server side, but I need the jsonArray to fill some more html elements which the client can discreetly modify as per his/her wish. Commented Nov 16, 2018 at 14:56

2 Answers 2

1

Since it is of array type you can access it with its Index and here it is 0 as the array element is only one.

presetArray[0].BaseFormat
presetArray[0].ColorFormat

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



console.log("Base Format: " + presetArray[0].BaseFormat);
console.log("Color Format: " + presetArray[0].ColorFormat);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

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.