具有多个根对象的GXT 3.0 JsonReader

时间:2012-08-27 02:59:06

标签: json gwt gxt

我开始使用GXT,并且对如何使用JsonReader解析具有多个根对象的有效json字符串感到有点困惑。我有一组选择框来构建sql语句以显示网格中的记录。我也从数据库中获取选择的值。我正在尝试做的是对我的php数据库函数的单个请求,以在一个字符串中构建具有选择框的所有值的json。我的第一个想法是JsonReader。

以下是我正在使用的json的一个例子:

"categories":[{"id":"1","value":"categoryValue1"},{"id":"2","value":"categoryValue2"}], "frequencies":[{"id":"1","value":"frequencyValue1"},{"id":"2","value":"frequencyValue2"}]

建立api中的cityList示例,这是我到目前为止所做的。

JsonRootObject接口:

public interface JsonRootObject {
  List<SelectionProperties> getCategories();
  List<SelectionProperties> getFrequencies();
}

JsonRootObjectAutoBeanFactory:

public interface JsonRootObjectAutoBeanFactory extends AutoBeanFactory {
  AutoBean<JsonRootObject> jsonRootObject();
}

我创建了一个SelectionProperties接口,因为它们都是单个int / string值对:

public interface SelectionProperties {
    String getId();
    String getValue();
}

现在,根据api:

   // To convert from JSON data, extend a JsonReader and override
   // createReturnData to return the desired type.

所以我为类别和频率创建了一个阅读器; CategoryReader:

public class CategoryReader
    extends
    JsonReader<ListLoadResult<SelectionProperties>, JsonRootObject> {

public CategoriesReader(AutoBeanFactory factory, Class<JsonRootObject> rootBeanType) {
    super(factory, rootBeanType);
}

protected ListLoadResult<SelectionProperties> createReturnData(Object loadConfig, JsonRootObject incomingData) {
    return new ListLoadResultBean<SelectionProperties>(incomingData.getCategories());
  }
}

FrequencyReader:

    public class FrequencyReader        extends
JsonReader<ListLoadResult<SelectionProperties>, net.apoplectic.testapps.client.JsonRootObject> {

public FrequencyReader(AutoBeanFactory factory, Class<JsonRootObject> rootBeanType) {
super(factory, rootBeanType);
}

protected ListLoadResult<SelectionProperties> createReturnData(Object loadConfig, JsonRootObject incomingData) {
    return new ListLoadResultBean<SelectionProperties>(incomingData.getFrequencies());
    }
}

这感觉不太对劲。我正在创建基本相同代码的多个实例并解析实际的json字符串两次(此时。一旦挖掘,我可能有更多的网格选项)。我的问题是我错过了什么或者是否有更有效的方法来解析响应字符串?来自我的onSuccessfulResponse:

        JsonRootObjectAutoBeanFactory factory = GWT.create(JsonRootObjectAutoBeanFactory.class);
    CategoryReader catReader = new CategoriesReader(factory, JsonRootObject.class);
    FrequencyReader freqReader = new FrequenciesReader(factory, JsonRootObject.class);
    categories = catReader.read(null, response);
    frequencies = freqReader.read(null, response);
    List<SelectionProperties> categoriesList = categories.getData();
    List<SelectionProperties> frequenciesList = frequencies.getData();
    ListBox cateBox = new ListBox(false);
    ListBox freqBox = new ListBox(false);

    for (SelectionProperties category : categoriesList )
    {
        cateBox.addItem(category.getValue(), category.getId());
    }

    for (SelectionProperties frequency : frequenciesList)
    {
        freqBox.addItem(frequency.getValue(), frequency.getId());
    }

以上工作正常,两个ListBox都正确填充。我只是想知道这是否是正确的方法。提前谢谢!

0 个答案:

没有答案