This question follows from my own question here,
这个问题来自我自己的问题,
Getting JSON response into String in Java?
在Java中将JSON响应转换为String?
Well, I did get the expected result. Now If I want to extract the specific part of JSON response and use it to say, save it in a database, how would I do that?
好吧,我确实得到了预期的结果。现在,如果我想提取JSON响应的特定部分并使用它来说,将其保存在数据库中,我该怎么做?
I am thinking of using Jackson JSON parser. I checked the tutorials, but it is using only a file not a HTTP request.
我正在考虑使用Jackson JSON解析器。我检查了教程,但它只使用了一个文件而不是HTTP请求。
7 个解决方案
#1
2
Jackson is an excellent choice. I don't know what you gleaned from the tutorials; you might have to read beyond them. Jackson is a good Java library for manipulating JSON.
杰克逊是一个很好的选择。我不知道你从教程中收集到了什么;你可能需要阅读超越它们。 Jackson是一个用于操作JSON的优秀Java库。
#2
0
Gson
is an excellent json parser. You can use that to convert json to Java objects.
Gson是一个优秀的json解析器。您可以使用它将json转换为Java对象。
Gson gson = new Gson();
Foo foo = gson.fromJson("{your json string}", Foo.class);
If you're uncertain on what the Foo
class should look like you can always create Foo
as how you think it should look and serialize that to compare against your json string.
如果你不确定Foo类应该是什么样子,你总是可以创建Foo,就像你认为它看起来一样,并序列化它以与你的json字符串进行比较。
Gson gson = new Gson();
gson.toJson(new Foo());
#3
0
you can pass a Byte instead of a file. Check out the Jackson API
您可以传递字节而不是文件。查看Jackson API
Can you get the parameter(s) from the request and turn them into byte arrays?
你能从请求中获取参数并将它们转换为字节数组吗?
String source = "" +request.getParameter("param1");
byte[] byteArray = source.getBytes("UTF-8");
#4
0
I believe this is what you're looking for. Note in the comment it says // or URL, Stream, Reader, String, byte[]
meaning that any form of input stream could be used. Just get the input stream from the HTTP connection you're using and parse away.\
我相信这就是你要找的东西。注释在注释中说//或者URL,Stream,Reader,String,byte []意味着可以使用任何形式的输入流。只需从您正在使用的HTTP连接获取输入流并解析即可。
Actually same sort of thing can be done with the tutorial you might have read as well (with object mapping). The methods for reading the JSON stream, be it String, InputStream, or whatever, are overloaded to take multiple inputs and map them to a corresponding object.
实际上,您可能已经阅读过的教程(使用对象映射)可以完成同样的事情。读取JSON流的方法,无论是String,InputStream还是其他,都会被重载以获取多个输入并将它们映射到相应的对象。
#5
0
Supposed JSON is a simple plain object and readable with a string. Just convert the inputstream to a plain string in a correct chart set, then you could parse the string with Jackson mapper i think.
假设JSON是一个简单的普通对象,可以用字符串读取。只需将输入流转换为正确的图表集中的普通字符串,然后您就可以使用杰克逊映射器解析该字符串。
getMapper().readValue(dataStr, Class.class);
#6
0
I assume that your entire response is a json and not only part of it.
我假设你的整个回应都是json而不仅仅是它的一部分。
You could try Genson http://code.google.com/p/genson/. It has most of jackson features plus other ones that are quite usefull, Genson is also easier to use and tries to be more open to extension than jackson. Here is a solution where you will use the databinding capabilities of Genson to deserialize your stream into a java object and then you would work with your object and do whatever you want.
你可以试试Genson http://code.google.com/p/genson/。它具有大多数杰克逊功能以及其他非常有用的功能,Genson也更容易使用,并试图比杰克逊更加开放。这是一个解决方案,您将使用Genson的数据绑定功能将您的流反序列化为java对象,然后您将使用您的对象并执行您想要的任何操作。
InputStream instream = entity.getContent();
Genson genson = new Genson();
ObjectReader reader = genson.createReader(instream);
DeserializeIntoThisClass myObject = genson.deserialize(DeserializeIntoThisClass.class, reader, new Context(genson));
// and now you can work with this object and store it in a database if you want
#7
0
You can pass URL
instead of File
, which you could find from Javadocs, so:
你可以传递URL而不是File,你可以从Javadocs找到它,所以:
MyObject ob = new ObjectMapper().readValue(url, MyObject.class);
#1
2
Jackson is an excellent choice. I don't know what you gleaned from the tutorials; you might have to read beyond them. Jackson is a good Java library for manipulating JSON.
杰克逊是一个很好的选择。我不知道你从教程中收集到了什么;你可能需要阅读超越它们。 Jackson是一个用于操作JSON的优秀Java库。
#2
0
Gson
is an excellent json parser. You can use that to convert json to Java objects.
Gson是一个优秀的json解析器。您可以使用它将json转换为Java对象。
Gson gson = new Gson();
Foo foo = gson.fromJson("{your json string}", Foo.class);
If you're uncertain on what the Foo
class should look like you can always create Foo
as how you think it should look and serialize that to compare against your json string.
如果你不确定Foo类应该是什么样子,你总是可以创建Foo,就像你认为它看起来一样,并序列化它以与你的json字符串进行比较。
Gson gson = new Gson();
gson.toJson(new Foo());
#3
0
you can pass a Byte instead of a file. Check out the Jackson API
您可以传递字节而不是文件。查看Jackson API
Can you get the parameter(s) from the request and turn them into byte arrays?
你能从请求中获取参数并将它们转换为字节数组吗?
String source = "" +request.getParameter("param1");
byte[] byteArray = source.getBytes("UTF-8");
#4
0
I believe this is what you're looking for. Note in the comment it says // or URL, Stream, Reader, String, byte[]
meaning that any form of input stream could be used. Just get the input stream from the HTTP connection you're using and parse away.\
我相信这就是你要找的东西。注释在注释中说//或者URL,Stream,Reader,String,byte []意味着可以使用任何形式的输入流。只需从您正在使用的HTTP连接获取输入流并解析即可。
Actually same sort of thing can be done with the tutorial you might have read as well (with object mapping). The methods for reading the JSON stream, be it String, InputStream, or whatever, are overloaded to take multiple inputs and map them to a corresponding object.
实际上,您可能已经阅读过的教程(使用对象映射)可以完成同样的事情。读取JSON流的方法,无论是String,InputStream还是其他,都会被重载以获取多个输入并将它们映射到相应的对象。
#5
0
Supposed JSON is a simple plain object and readable with a string. Just convert the inputstream to a plain string in a correct chart set, then you could parse the string with Jackson mapper i think.
假设JSON是一个简单的普通对象,可以用字符串读取。只需将输入流转换为正确的图表集中的普通字符串,然后您就可以使用杰克逊映射器解析该字符串。
getMapper().readValue(dataStr, Class.class);
#6
0
I assume that your entire response is a json and not only part of it.
我假设你的整个回应都是json而不仅仅是它的一部分。
You could try Genson http://code.google.com/p/genson/. It has most of jackson features plus other ones that are quite usefull, Genson is also easier to use and tries to be more open to extension than jackson. Here is a solution where you will use the databinding capabilities of Genson to deserialize your stream into a java object and then you would work with your object and do whatever you want.
你可以试试Genson http://code.google.com/p/genson/。它具有大多数杰克逊功能以及其他非常有用的功能,Genson也更容易使用,并试图比杰克逊更加开放。这是一个解决方案,您将使用Genson的数据绑定功能将您的流反序列化为java对象,然后您将使用您的对象并执行您想要的任何操作。
InputStream instream = entity.getContent();
Genson genson = new Genson();
ObjectReader reader = genson.createReader(instream);
DeserializeIntoThisClass myObject = genson.deserialize(DeserializeIntoThisClass.class, reader, new Context(genson));
// and now you can work with this object and store it in a database if you want
#7
0
You can pass URL
instead of File
, which you could find from Javadocs, so:
你可以传递URL而不是File,你可以从Javadocs找到它,所以:
MyObject ob = new ObjectMapper().readValue(url, MyObject.class);