jquery$.getJSON-处理结果类型

jquery$.getJSON-处理结果类型,jquery,Jquery,使用$.getJSON,我的结果可以是空的、单个值或值列表 如果结果包含单个值,我可以通过result.someReturnedValue访问数据。但是,如果结果包含列表,我必须使用数组语法循环结果数据以访问数据,例如result[i]。someReturnedValue 现在我不想编写两次相同的代码来处理每种类型的结果。(我将结果附加到DOM元素,当我必须考虑这两个符号时,代码变得越来越大)是否有一种通用的方法可以处理结果以防止这种重复 单一结果的例子 $('div[id="'+div

使用$.getJSON,我的结果可以是空的、单个值或值列表

如果结果包含单个值,我可以通过result.someReturnedValue访问数据。但是,如果结果包含列表,我必须使用数组语法循环结果数据以访问数据,例如result[i]。someReturnedValue

现在我不想编写两次相同的代码来处理每种类型的结果。(我将结果附加到DOM元素,当我必须考虑这两个符号时,代码变得越来越大)是否有一种通用的方法可以处理结果以防止这种重复

单一结果的例子

    $('div[id="'+divId+'"]').append("<td>" + result.someValue1+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result.someValue2+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result.someValue2+ "</td>");
$('div[id=“”+divId+”).append(“+result.someValue1+”);
$('div[id=“”+divId+”).append(“+result.someValue2+”);
$('div[id=“”+divId+”).append(“+result.someValue2+”);
多个结果的示例:

    $('div[id="'+divId+'"]').append("<td>" + result[i].someValue1+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result[i].someValue2+ "</td>");
$('div[id="'+divId+'"]').append("<td>" + result[i].someValue2+ "</td>");
$('div[id=“”+divId+“]”)。追加(“+result[i].someValue1+”);
$('div[id=“”+divId+“]”)。追加(“+result[i].someValue2+”);
$('div[id=“”+divId+“]”)。追加(“+result[i].someValue2+”);

让JSON始终返回一个对象或数组(如果没有结果,它可能为空)

然后,您可以迭代内容,而无需使用特殊情况下的零、一或多个元素(您将始终使用现在的“多个结果”代码路径)。如果您确实需要特殊情况,它也将是微不足道的:

switch(result.length) {
    case 0:
    case 1: // etc
}

让JSON始终返回一个对象或数组(如果没有结果,它可能为空)

然后,您可以迭代内容,而无需使用特殊情况下的零、一或多个元素(您将始终使用现在的“多个结果”代码路径)。如果您确实需要特殊情况,它也将是微不足道的:

switch(result.length) {
    case 0:
    case 1: // etc
}
  • result
    始终是一个数组,即使它是空的或只有一个元素
  • result[i]
    包含一个数组而不是按顺序编号的属性,并遍历它
      • result
        始终是一个数组,即使它是空的或只有一个元素
      • result[i]
        包含一个数组而不是按顺序编号的属性,并遍历它

        • 好的,我找到了这个问题的解决方案。您可以创建自己的提供程序类,Jersey在生成JSON响应时将使用该类

          @Component
          @Provider
          public class 
          
          JAXBContextResolver implements ContextResolver<JAXBContext> {
          
          private final JAXBContext context;
          
          private final Class[] cTypes = { SomeCustomClass.class };
          
          public JAXBContextResolver() throws JAXBException {
              this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
                      cTypes);
          }
          
          public JAXBContext getContext(Class<?> objectType) {
              for (Class type : cTypes) {
                  if (type == objectType) {
                      return context;
                  }
              }
              return null;
          }
          
          @组件
          @提供者
          公共课
          JAXBContextResolver实现ContextResolver{
          私有上下文上下文;
          私有最终类[]cTypes={SomeCustomClass.Class};
          公共JAXBContextResolver()抛出JAXBEException{
          this.context=new JSONJAXBContext(JSONConfiguration.natural().build(),
          cTypes);
          }
          公共JAXBContext getContext(类objectType){
          对于(类类型:cTypes){
          if(type==objectType){
          返回上下文;
          }
          }
          返回null;
          }
          
          此提供程序使用自然JSON表示法,该表示法按照列表格式的要求序列化对象


          通过包含必要的注释/bean定义,确保Spring知道该类。好的,我找到了这个问题的解决方案。您可以创建自己的提供程序类,Jersey将在生成JSON响应时使用该类

          @Component
          @Provider
          public class 
          
          JAXBContextResolver implements ContextResolver<JAXBContext> {
          
          private final JAXBContext context;
          
          private final Class[] cTypes = { SomeCustomClass.class };
          
          public JAXBContextResolver() throws JAXBException {
              this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
                      cTypes);
          }
          
          public JAXBContext getContext(Class<?> objectType) {
              for (Class type : cTypes) {
                  if (type == objectType) {
                      return context;
                  }
              }
              return null;
          }
          
          @组件
          @提供者
          公共课
          JAXBContextResolver实现ContextResolver{
          私有上下文上下文;
          私有最终类[]cTypes={SomeCustomClass.Class};
          公共JAXBContextResolver()抛出JAXBEException{
          this.context=new JSONJAXBContext(JSONConfiguration.natural().build(),
          cTypes);
          }
          公共JAXBContext getContext(类objectType){
          对于(类类型:cTypes){
          if(type==objectType){
          返回上下文;
          }
          }
          返回null;
          }
          
          此提供程序使用自然JSON表示法,该表示法按照列表格式的要求序列化对象


          通过包含必要的注释/bean定义来确保Spring知道该类

          我从服务器返回一个列表,但是如果它只包含一个条目,它将在我的JSON中产生一个结果response@linuxlewis:如何生成JSON?使用Jersey Rest API中的JSONWithPadding对象包装包含列表的自定义对象解决方案是添加一个自定义提供程序,该提供程序覆盖Jersey用于创建JSON响应的默认JSON注释。我将从服务器返回一个列表。但是,如果它只包含一个条目,则会在my JSON中生成一个结果response@linuxlewis:如何生成JSON?使用Jersey Rest API中的JSONWithPadding对象包装自定义包含列表的对象解决方案是添加一个自定义提供程序,该提供程序覆盖Jersey用于创建JSON响应的默认JSON注释