search

Home  >  Q&A  >  body text

android - 关于JSON格式转为JavaBean?

[
    {
        "url": "http://www.baidu.com",
        "title": "测试1",
        "type": 1,
        "pic": "null"
    },
    {
        "url": "http://www.baidu.com",
        "title": "测试2",
        "type": 2,
        "pic": "null"
    }
]

例如如上的JSON格式,其对应的JavaBean是什么样子的?
[]括号括起来的应该是对应List,但是其List并没有一个key,这该怎么弄?
Gson解析就报这里错了。。。

大家讲道理大家讲道理2897 days ago446

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:45:18

    This is a problem of converting JOSN array to List. For example, the object is as follows

     public class TestBean{
         private String url;
         private String title;
         private String type;
         private String pic;
         //省略getter,setter
    }

    The json string you gave you converted into a list should be:
    List<TestBean> list = gson.fromJson(str, new TypeToken<List<TestBean>>(){}.getType());

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:45:18

    public class Test{
        
        private List<TestBean> tests;
        //省略getter,setter
        
        public static class TestBean{
             private String url;
             private String title;
             private String type;
             private String pic;
             //省略getter,setter
        }
    }
    
    //这就能得到如题所示的json了
    JSON.toJSON(Test.getTests())

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:45:18

    In json, [] represents an array and {} represents an object. In the above structure, there are two objects in the array, and each object has four properties. The data structure is

    List<Object> list = new ArrayList<Object>();
    

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:45:18

    http://git.oschina.net/angryi...

    reply
    0
  • Cancelreply