我想创建一个Restful网络服务,它将生成JSON输出... 因为它应该包含一些像下面这样的图像URL以及json数组和json对象的数量,..
{
"worldpopulation":
[
{
"rank":1,"country":"China",
"population":"1,354,040,000",
"flag":"http://www.androidbegin.com/tutorial/flag/china.png"
},
{
"rank":2,"country":"India",
"population":"1,210,193,422",
"flag":"http://www.androidbegin.com/tutorial/flag/india.png"
},
{
"rank":3,"country":"United States",
"population":"315,761,000",
"flag":"http://www.androidbegin.com/tutorial/flag/unitedstates.png"
}
]
}
所以我以后可以在移动设备上使用它,......
答案 0 :(得分:1)
您的Google是否已关闭!!! Google提供了许多与REST网络服务相关的好东西。
无论如何看看这个东西
<强> Building a Simple RESTful Web Service to produce JSON using Jersey 强>
<强> Developing REST Web Services in Eclipse 强>
要创建JSON,请参阅此示例
假设您要创建一个JSON,如下所示
{
"worldpopulation":
[
{
"rank":1,"country":"China",
"population":"1,354,040,000",
"flag":"http://www.androidbegin.com/tutorial/flag/china.png"
},
{
"rank":2,"country":"India",
"population":"1,210,193,422",
"flag":"http://www.androidbegin.com/tutorial/flag/india.png"
},
{
"rank":3,"country":"United States",
"population":"315,761,000",
"flag":"http://www.androidbegin.com/tutorial/flag/unitedstates.png"
}
]
}
用于创建上述JSON的java代码如下所示
JSONObject jsonobj=new JSONObject();
JSONArray obj = new JSONArray();
HashMap rows=new HashMap();
rows.put("rank","1");
rows.put("country","China");
rows.put("population","1,354,040,000");
rows.put("flag","http://www.androidbegin.com/tutorial/flag/china.png");
obj.put(rows);
rows=new HashMap();
rows.put("rank","2");
rows.put("country","India");
rows.put("population","1,210,193,422");
rows.put("flag","http://www.androidbegin.com/tutorial/flag/india.png");
obj.put(rows);
rows=new HashMap();
rows.put("rank","3");
rows.put("country","United States");
rows.put("population","315,761,000");
rows.put("flag","http://www.androidbegin.com/tutorial/flag/unitedstates.png");
jsonobj.put("worldpopulation", obj);
System.out.println(jsonobj.toString());
答案 1 :(得分:1)
你可以做的第一件快速的事情就是为了一个tomcat“你好世界”而谷歌,并改变它以将你的json作为一个硬编码的字符串返回。
我的第一个google结果是此链接http://www-inf.it-sudparis.eu/~nguyen_n/teaching_assistant/servlet-jsp/helloworld-tomcat
在那里你可以改变像:
这样的行out.println("<HTML>");
就像
out.println("{
\"worldpopulation\":
[
{\"
....");