none
VB.Net - The type initializer for 'System.Web.Helpers.Json' threw an exception ( Json.Encode ) RRS feed

  • Question

  • Hi, I get "The type initializer for 'System.Web.Helpers.Json' threw an exception." when running below code.

    MSDN Link

    Imports Newtonsoft.Json
    Imports System.Text
    Imports System.Web.Helpers
    Imports System.Web.HttpUtility
    Imports System.Web
    Imports System
    Imports System.IO 
    
    Dim value As Object
    
    
            value = Me.TextBox1.Text
    
            TextBox2.Text = (System.Web.Helpers.Json.Encode(value))

    Tuesday, May 16, 2017 5:05 PM

Answers

  • Hi LetMeCode,

    In order to workaround this problem you need to make sure "Enable the Visual Studio hosting process" is unchecked in your project's settings under Debug.

    Hope it is helpful to you.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by LetMeCode Wednesday, May 17, 2017 7:57 AM
    Wednesday, May 17, 2017 6:46 AM
    Moderator

All replies

  • Not for me


    Success
    Cor

    Tuesday, May 16, 2017 5:32 PM
  • LMC,

    You've got Newtonsoft, so let it do the work as shown here:

    http://www.newtonsoft.com/json/help/html/SerializingJSON.htm

    For example:

    Option Strict On Option Explicit On Option Infer Off Imports Newtonsoft.Json Public Class Form1 Private Class ExampleClass Public Property Text As String End Class Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load Const text As String = _ "This is a string to be serialized to JSON" Dim output As String = JsonConvert.SerializeObject(text) Stop Dim ec As New ExampleClass _ With {.Text = "This is also a string..."} output = JsonConvert.SerializeObject(ec) Stop End Sub End Class


    Each time the code gets to "Stop", hover your mouse over the variable named "output" and you'll see the value.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Tuesday, May 16, 2017 6:08 PM ...modified link
    Tuesday, May 16, 2017 6:05 PM
  • Hi LetMeCode,

    In order to workaround this problem you need to make sure "Enable the Visual Studio hosting process" is unchecked in your project's settings under Debug.

    Hope it is helpful to you.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by LetMeCode Wednesday, May 17, 2017 7:57 AM
    Wednesday, May 17, 2017 6:46 AM
    Moderator
  • Thanks Cherry Bu.

    =============

    @ Anyone

    What is the difference between (Json.Encode() / JavaScriptStringEncode()) method?

    If there's no difference, why Microsoft made two methods to do the same thing?

    Wednesday, May 17, 2017 8:02 AM