2

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.

4

1 回答 1

1

字典有利于查找,我不知道 VB 语法,但会解释你将能够在 VB 中实现相同的 c#

创建一个模型类

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

填充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)

希望有帮助

于 2012-11-07T19:05:00.017 回答