如何从响应中提取json数据 - Java

时间:2014-10-10 04:33:30

标签: 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 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();
}
}

谢谢!!!!!

@Ankur: 这就是我尝试的方式,

This is one of your second ways... I called the function from where I wanted and passed the variable "line" containing the json string

@ Lahiru Prasanna,@ ankur-singhal 非常感谢。!!

2 个答案:

答案 0 :(得分:2)

实现这一目标的方法很少。

1。)创建response pojo

MyResponse ob = new ObjectMapper().readValue(jsonString, MyResponse.class);

//然后在上面调用getter。

2。)获取key/values

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);
}

<强>输出

json : {"name":"Frank","food":"pizza","quantity":3}
map : {food=pizza, name=Frank, quantity=3}

答案 1 :(得分:2)

我认为您成功获得了HttpResponse.here变量,称为响应是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");