nutzam / nutz

Nutz -- Web Framework(Mvc/Ioc/Aop/Dao/Json) for ALL Java developer
https://nutzam.com
Apache License 2.0
2.54k stars 943 forks source link

需求:NutMap 对于json可变的结果集,可通过getAs强制得到List集合 #1259

Open Wizzercn opened 7 years ago

Wizzercn commented 7 years ago
{
  "key":{
    "a":"aaaa0";
        "b":"bbbb0";
  }
}

{
  "key":{
    "a":"aaaa1";
        "b":"bbbb1";
  },
  "key":{
    "a":"aaaa2";
        "b":"bbbb2";
  }
}
NutMap json = Json.fromJson(NutMap.class, txt);

json.getAs("key",List.class);
wendal commented 7 years ago
{
  "key":[
       {
      "a":"aaaa1",
          "b":"bbbb1",
      },
     {
       "a":"aaaa2",
           "b":"bbbb2",
     }]
}
Wizzercn commented 7 years ago

@wendal 亲,数据接口是电信的,我改不了人家的接口啊,它就是这样BT的啊

wendal commented 7 years ago

JSON里面带重复key???

Wizzercn commented 7 years ago

@wendal 准确的说是xml:

<root>
    <web:NEW_DATA_TICKET_QRsp>
        <number>1064901037***</number>
        <TOTAL_BYTES_CNT>33.97MB</TOTAL_BYTES_CNT>
        <DURATION_CNT_CH>479小时53分钟45秒</DURATION_CNT_CH>
        <CHARGE_CNT_CH>0(元)</CHARGE_CNT_CH>
        <NEW_DATA_TICKET_QRlist>
            <TICKET_NUMBER>1</TICKET_NUMBER>
            <TICKET_TYPE>江苏南京</TICKET_TYPE>
            <SERVICE_TYPE>3G(EVDO)</SERVICE_TYPE>
            <START_TIME>2014-10-01 03:43</START_TIME>
            <DURATION_CH>6小时2秒</DURATION_CH>
            <TICKET_CHARGE_CH>0.00</TICKET_CHARGE_CH>
            <BYTES_CNT>267KB</BYTES_CNT>
            <CCG_PRODUCT_NAME />
        </NEW_DATA_TICKET_QRlist>
        <NEW_DATA_TICKET_QRlist>
            <TICKET_NUMBER>2</TICKET_NUMBER>
            <TICKET_TYPE>江苏南京</TICKET_TYPE>
            <SERVICE_TYPE>3G(EVDO)</SERVICE_TYPE>
            <START_TIME>2014-10-01 09:43</START_TIME>
            <DURATION_CH>6小时</DURATION_CH>
            <TICKET_CHARGE_CH>0.00</TICKET_CHARGE_CH>
            <BYTES_CNT>290KB</BYTES_CNT>
            <CCG_PRODUCT_NAME />
        </NEW_DATA_TICKET_QRlist>
        <NEW_DATA_TICKET_QRlist>
            <TICKET_NUMBER>3</TICKET_NUMBER>
            <TICKET_TYPE>江苏南京</TICKET_TYPE>
            <SERVICE_TYPE>3G(EVDO)</SERVICE_TYPE>
            <START_TIME>2014-10-01 15:43</START_TIME>
            <DURATION_CH>6小时</DURATION_CH>
            <TICKET_CHARGE_CH>0.00</TICKET_CHARGE_CH>
            <BYTES_CNT>296KB</BYTES_CNT>
            <CCG_PRODUCT_NAME />
        </NEW_DATA_TICKET_QRlist>
        <IRESULT>0</IRESULT>
        <SMSG>成功</SMSG>
    </web:NEW_DATA_TICKET_QRsp>
</root>
Kerbores commented 7 years ago

xpath

wendal commented 7 years ago

NutMap.getList会自动封装的

Wizzercn commented 7 years ago

问题是,有一个结果的时候封装成了对象,多个的时候变成list,我是想不管一个或多个都是list

wendal commented 7 years ago

哦,这是个问题

wendal commented 7 years ago

问题又来了, nutz如何知道某个属性是否应该是list呢...

Lang.map2obj的里面做些判断??

Wizzercn commented 7 years ago
List<NutMap> cumulRspList = new ArrayList<>();
                        Object v = web.get("CumulRspList");
                        if (v instanceof Collection) {
                            cumulRspList = web.getList("CumulRspList", NutMap.class);
                        }
                        if (v instanceof NutMap) {
                            cumulRspList.add((NutMap) v);
                        }

暂时是这样解决的