如何从响应中提取json数据-Java
我试图从Json字符串中提取数据,该字符串是通过只使用Java代码的响应获得的。 我在这里发布我的Java代码 输出:如何从响应中提取json数据-Java,java,json,response,Java,Json,Response,我试图从Json字符串中提取数据,该字符串是通过只使用Java代码的响应获得的。 我在这里发布我的Java代码 输出: Entering into while loop [{"name":"Frank","food":"pizza","quantity":3}] 这是我的Java代码 public void receive() { System.out.println("Entering into sendNote method"); try { // make json st
Entering into while loop
[{"name":"Frank","food":"pizza","quantity":3}]
这是我的Java代码
public void receive()
{
System.out.println("Entering into sendNote method");
try {
// make json string,
String json = "{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}";
// send as http get request
URL url1 = new URL("http://myurl/file.php?usersJSON="+userList);
URLConnection conn1= url1.openConnection();
//I am receiving exactly what I have sent....
BufferedReader rd = new BufferedReader(new InputStreamReader(conn1.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
System.out.println("Entering into while loop");
System.out.println(line);// line contains the received json parameters
//I want to enter the recieved parameters into my database
//
//
//
//I need the solution right here....
}
rd.close();
}
catch (Exception e)
{
System.out.println("Error Occured while receiving");
e.printStackTrace();
}
}
谢谢
@安克尔:
我就是这样试的
@Lahiru Prasanna,@ankur singhal
非常感谢 实现这一目标的方法很少 1.)创建一个
响应pojo
MyResponse ob = new ObjectMapper().readValue(jsonString, MyResponse.class);
//然后在上面调用getters
2.)获取键/值
JSONObject json = (JSONObject)new JSONParser().parse(""name":"Frank","food":"pizza","quantity":3}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("food"));
3.)将Json
转换为HashMap
public static void main(String[] args) {
try {
jsonToMap("{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void jsonToMap(String t) throws JSONException {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
System.out.println("json : " + jObject);
System.out.println("map : " + map);
}
实现这一目标的方法很少 1.)创建一个
响应pojo
MyResponse ob = new ObjectMapper().readValue(jsonString, MyResponse.class);
//然后在上面调用getters
2.)获取键/值
JSONObject json = (JSONObject)new JSONParser().parse(""name":"Frank","food":"pizza","quantity":3}");
System.out.println("name=" + json.get("name"));
System.out.println("width=" + json.get("food"));
3.)将Json
转换为HashMap
public static void main(String[] args) {
try {
jsonToMap("{\"name\":\"Frank\",\"food\":\"pizza\",\"quantity\":3}");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void jsonToMap(String t) throws JSONException {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
System.out.println("json : " + jObject);
System.out.println("map : " + map);
}
我认为您成功地获得了HttpResponse。这里称为response的变量是HttpResponse
// Could do something better with response.
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(builder.toString());
} catch (JSONException e) {
System.out.println("Error parsing data " + e.toString());
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
System.out.println( "" + e.toString());
System.out.println("" + e.toString());
}
重要的是永远不要在json操作中使用字符串
您可以像这样从jsonObject检索数据
String name = jsonObject.getString("name");
我认为您成功地获得了HttpResponse。这里称为response的变量是HttpResponse
// Could do something better with response.
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
content.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONObject jsonObject = new JSONObject(builder.toString());
} catch (JSONException e) {
System.out.println("Error parsing data " + e.toString());
}
}
} catch (Exception e) {
System.out.println("Error: " + e.toString());
System.out.println( "" + e.toString());
System.out.println("" + e.toString());
}
重要的是永远不要在json操作中使用字符串
您可以像这样从jsonObject检索数据
String name = jsonObject.getString("name");
你为什么不使用谷歌GSONAPI呢?()为您的请求使用第三方JSON api,称为GSON。您也可以使用java提供的JSONObject支持,但如果我得到代码的精确实现,这可能会有所帮助?()为您的请求使用第三方JSON api,称为GSON。您也可以使用java提供的JSONObject支持,但如果我得到了代码的精确实现,这可能会有所帮助。。我也无法解决一些错误。@Kaage所有3个都应该可以工作,您会遇到哪些错误。我应该将MyResponse ob=new ObjectMapper().readValue(jsonString,MyResponse.class)???在哪里调用jsonToMap函数??我有点困惑,对不起。@Kaage首先我给了你三种不同的方法,你有json字符串,你可以在任何方法中传递它。好的,我试过使用第二种方法。但是当我运行文件“java.lang.ClassCastException:org.json.simple.JSONArray不能强制转换为org.json.simple.JSONObject”时,我遇到了这个错误。我试图将代码放入其中,但它似乎不起作用。。我也无法解决一些错误。@Kaage所有3个都应该可以工作,您会遇到哪些错误。我应该将MyResponse ob=new ObjectMapper().readValue(jsonString,MyResponse.class)???在哪里调用jsonToMap函数??我有点困惑,对不起。@Kaage首先我给了你三种不同的方法,你有json字符串,你可以在任何方法中传递它。好的,我试过使用第二种方法。但是,当我运行文件“java.lang.ClassCastException:org.json.simple.JSONArray不能强制转换为org.json.simple.JSONObject”时,出现了这个错误,该文件显示了StatusLine、HttpEntity、builder.toString()和jsoneException处的错误。使用eclipe或netbeans没有什么重要的吗?单击灯泡图标连接表示。。。气泡按钮未显示import option.import org.apache.http.StatusLine;导入org.apache.http.HttpEntity;导入org.json.JSONArray;导入org.json.JSONException;添加这些行很抱歉,您必须导入外部库,在StatusLine、HttpEntity、builder.toString()和JSONException处显示错误。使用eclipe或netbeans没有什么重要的吗?单击灯泡图标连接表示。。。气泡按钮未显示import option.import org.apache.http.StatusLine;导入org.apache.http.HttpEntity;导入org.json.JSONArray;导入org.json.JSONException;添加这些行很抱歉,您必须导入外部库