【问题标题】:json string having id and name? using c sharp具有id和名称的json字符串?使用升c
【发布时间】:2016-02-15 14:01:18
【问题描述】:

这是我的 json 字符串

 var people = '{"Id": 101020001 ,  "Title": "subsidary","Id": 103010001 ,  "Title": "LOAN FROM BANK ?","Id": 104010001 ,  "Title": "FAWAD","Id": 104010002 ,  "Title": "GETING","Id": 104010003}

我把它构建成

     string MyDictionaryToJson(Dictionary<int, string > dict)
       {
     var entries = dict.Select(d =>
         string.Format("\"Id\": {0} ,  \"Title\": \"{1}\"", d.Key, string.Join(",", d.Value)));
     return "{" + string.Join(",", entries) + "}";
 }

但我想填充剑道网格,所以不知道 ID 我想要这样

"{"Id":"101020001" , "Title":"titlesubsidary"},{"Id":"101020002" , "Title":"titlenext"}, {"Id":"101020003" , "Title":"titleagain"}

剑道组合框

    var people = '<%=jsonS %>'
  $('#Text1').kendoComboBox({

       dataTextField: 'Title',
      dataValueField: 'Id',
      template: "<table><tr><td width='100px'>${ Id}</td><td width='100px'>${ Title }</td></tr></table>",
      dataSource: { data: people }

  });

error:ReferenceError: Id 未定义

【问题讨论】:

  • 因为您的 JSON 无效。使用 JSON.NET 创建 JSON
  • 不要尝试手动创建 json ......正如你的无效输出所见证的那样,它太容易出错
  • 我同意上面的陈述,你应该使用某种类型的序列化库,还有一点需要注意的是,你在 javascript 中将 Id 作为你的 dataValueField,但该属性称为 ID,且 D 大写。跨度>
  • 现在我已经更改了字符串格式,并且大写仍然说 id 缺少如何处理该 json 来实现 id

标签: javascript c# .net json kendo-ui


【解决方案1】:

因为您的 Json 字符串无效。 您不应该手动构建它。尝试使用Json.NET。 如果你想检查 Json 是如何映射到对象的,你可以使用json2csharp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多