james Makinde
james Makinde

Reputation: 983

Parse string to JArray in C# with method as property

I am storing JSON data in a SQL DB which returns a string, and I need to convert it to a JArray in C#. A property of one of the properties is not a string or number, but a method name and it throws an exception because JArray can't parse it. How do I process this correctly since that value cannot be enclosed in a string quotes and it won't work.

This is the JSON:

 {
      "id": "4",
      "data": [
    {     
      wrappers: ['floating-label'],     
      fieldGroup: [{
        key: 'town',
        type: 'input',
        templateOptions: {
          required: true,
          placeholder: "Your town",
         
        },
      }],
    },
    {
      key: 'Connection',
      type: 'select',
      templateOptions: {
        label: 'Connection',
        options: this.dataservice.getModels(),
        valueProp: 'id',
        labelProp: 'name',
      },
    },
  ]
}

My current code after getting the JSON from the DB: in a select

var resultArray =  JArray.FromObject(result.Select(e => JObject.Parse(e.Form)).ToArray());

Is there a way to convert to JArray or a JSON object, and still keep the value for options in the JSON above as is?

Upvotes: 0

Views: 366

Answers (1)

Mario Vernari
Mario Vernari

Reputation: 7306

Do not confuse JSON and JavaScript: your sample looks as JavaScript. JSON requires double-quotes only for strings and for field names (as in the beginning of your sample). Moreover, JSON allows only strings, numbers, and booleans.

Read the specs here.

Upvotes: 2

Related Questions