user1477388
user1477388

Reputation: 21440

Creating JSON key, value in VB.NET

I have the following code:

    Dim result = New Dictionary(Of String, String)

    For Each item In food

        result.Add(StrConv(item.Shrt_Desc.Replace(",", ", "), VbStrConv.ProperCase), item.Shrt_Desc)

    Next

    Return Json(result, JsonRequestBehavior.AllowGet)

I need to make it into the following key and value in JSON:

An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

How can I do this? Thank you.

Upvotes: 2

Views: 1717

Answers (1)

John x
John x

Reputation: 4031

Dictionaries are good for the look ups, i dont know the VB syntax but will explain the c# you will be able to achieve same in VB so

create a model class like

public class SomeClass{
 public string label{get;set;}
 public string value{get;set;}
}

populate the List

 IList<SomeClass> result = New List<SomeClass>();

    foreach(var item in food){
           result.Add(new SomeClass{
                  label=StrConv(item.Shrt_Desc.Replace(",", ", "),
                  value= VbStrConv.ProperCase
            });
}
 Return Json(result, JsonRequestBehavior.AllowGet)

hope that helps

Upvotes: 1

Related Questions