怎么用JAVA生成下面的JSON字符串, 本帖最后由 eimhee 于 2013-08-09 21:44:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是简单对象自己写个就争对性的就OK了你的这个相对复杂些,推荐使用Json-lib http://json-lib.sourceforge.net/ Map map = new HashMap(); map.put( "name", "json" ); map.put( "bool", Boolean.TRUE ); map.put( "int", new Integer(1) ); map.put( "arr", new String[]{"a","b"} ); map.put( "func", "function(i){ return this.arr[i]; }" ); JSONObject jsonObject = JSONObject.fromObject( map ); System.out.println( jsonObject ); 还是用代码生成下面的JSON吧 { "title":"Services added sale with image fix # 2", "type":"add_free_sale", "body":{ "und":[{ "value":"This is the body field" }]}, "field_address":{ "und":[{ "country":"US", "thoroughfare":"711 B Ave W", "locality":"Oskaloosa", "administrative_area":"IA", "postal_code":52577 }]}, "field_dates":{ "und":[{ "value":{"date":"10/17/2012"}, "value2":{"date":"10/17/2012"} }]}, "field_sale_type":{ "und":"Garage Sale"}, "name":"bot", "language":"und", "date":"2012-10-20", "field_image":{"und":[null]} } 我印象中用JSON库,JSONObject本身就有类似于put的方法的,一般还有一个叫JSONArray的是用来处理json数组的,两者都不需要map跟list的 用Jsonlib做吧。很方便的。信息可以设计成对应的类,也可以装在Map里面,推荐封装成类对象。 详细请看:http://download.csdn.net/detail/lxq_xsyu/5867435 用Bean对象进行数据绑定也是可以的。 何必Map List那么麻烦 json-lib不是有JSONObject 和JSONArray嘛 如何提取网页中的文字的码位 关于切换JDK版本的疑问 前段时间人家晒的一些小技巧,没啥好的,只是小整理了下,别失望 江湖告急!高手帮个啦! try catch 问题 java在命令方式改成jbuilder时,一些附带参数在jbuilder里面怎么设置参数 如何将Tree中的叶节点和DesktopPane中的各个InternalFrame联系起来 急求《java数据结构与算法分析》中文版 了解IO底层的高手进 在java中不用JDBC-ODBC桥怎样连接sql server数据库? Java菜鸟 有个小问题希望有时间的大神们帮下忙
你的这个相对复杂些,推荐使用Json-lib
http://json-lib.sourceforge.net/ Map map = new HashMap();
map.put( "name", "json" );
map.put( "bool", Boolean.TRUE );
map.put( "int", new Integer(1) );
map.put( "arr", new String[]{"a","b"} );
map.put( "func", "function(i){ return this.arr[i]; }" );
JSONObject jsonObject = JSONObject.fromObject( map );
System.out.println( jsonObject );
{
"title":"Services added sale with image fix # 2",
"type":"add_free_sale",
"body":{
"und":[{
"value":"This is the body field"
}]},
"field_address":{
"und":[{
"country":"US",
"thoroughfare":"711 B Ave W",
"locality":"Oskaloosa",
"administrative_area":"IA",
"postal_code":52577
}]},
"field_dates":{
"und":[{
"value":{"date":"10/17/2012"},
"value2":{"date":"10/17/2012"}
}]},
"field_sale_type":{
"und":"Garage Sale"},
"name":"bot",
"language":"und",
"date":"2012-10-20",
"field_image":{"und":[null]}
}
信息可以设计成对应的类,也可以装在Map里面,推荐封装成类对象。