如何在Java中转换或转换哈希图到JSON对象,并再次将JSON对象转换为JSON字符串?
当前回答
你可以使用XStream——它真的很方便。请看这里的例子
package com.thoughtworks.xstream.json.test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class WriteTest {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<String,String>();
map.add("1", "a");
map.add("2", "b");
XStream xstream = new XStream(new JettisonMappedXmlDriver());
System.out.println(xstream.toXML(map));
}
}
其他回答
对于使用TypeToken的更复杂的映射和列表,Gson是一种方式。getParameterized方法:
我们有一张这样的地图:
Map<Long, List<NewFile>> map;
我们使用上面提到的getParameterized方法来获取类型,如下所示:
Type listOfNewFiles = TypeToken.getParameterized(ArrayList.class, NewFile.class).getType();
Type mapOfList = TypeToken.getParameterized(LinkedHashMap.class, Long.class, listOfNewFiles).getType();
然后使用Gson对象fromJson方法,使用mapflist对象,像这样:
Map<Long, List<NewFile>> map = new Gson().fromJson(fileContent, mapOfList);
上面提到的对象NewFile看起来是这样的:
class NewFile
{
private long id;
private String fileName;
public void setId(final long id)
{
this.id = id;
}
public void setFileName(final String fileName)
{
this.fileName = fileName;
}
}
反序列化的JSON是这样的:
{ “1”:[ { “id”:12232年, “文件名”:“test.html” }, { “id”:12233年, “文件名”:“file.txt” }, { “id”:12234年, “文件名”:“obj.json” } ], “2”:[ { “id”:122321年, “文件名”:“test2.html” }, { “id”:122332年, “文件名”:“file2.txt” }, { “id”:122343年, “文件名”:“obj2.json” } ] }
如果你正在使用net.sf.json.JSONObject,那么你不会在其中找到JSONObject(map)构造函数。您必须使用公共静态JSONObject fromObject(对象对象)方法。该方法接受JSON格式的字符串、map、dynabean和javabean。
JSONObject jsonObject = JSONObject.fromObject(myMap);
你可以使用XStream——它真的很方便。请看这里的例子
package com.thoughtworks.xstream.json.test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
public class WriteTest {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<String,String>();
map.add("1", "a");
map.add("2", "b");
XStream xstream = new XStream(new JettisonMappedXmlDriver());
System.out.println(xstream.toXML(map));
}
}
我使用阿里巴巴fastjson,简单明了:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>VERSION_CODE</version>
</dependency>
和导入:
import com.alibaba.fastjson.JSON;
然后:
String text = JSON.toJSONString(obj); // serialize
VO vo = JSON.parseObject("{...}", VO.class); //unserialize
一切都好。
您只需枚举映射并将键-值对添加到JSONObject
方法:
private JSONObject getJsonFromMap(Map<String, Object> map) throws JSONException {
JSONObject jsonData = new JSONObject();
for (String key : map.keySet()) {
Object value = map.get(key);
if (value instanceof Map<?, ?>) {
value = getJsonFromMap((Map<String, Object>) value);
}
jsonData.put(key, value);
}
return jsonData;
}
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 查询JSON类型内的数组元素
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder