java curl json,如何使用curl在java和解析json响应

I have a curl command that gives a JSON response. I want to use this curl command in java & parse the JSON response in java. Is it possible to do so??

EDIT:

When I do the following:

public static void main(String[] args) throws Throwable {

try {

InputStream stream = Runtime.getRuntime().exec("curl --globoff --insecure --silent -u username:password -X GET -H 'Content-Type: application/json' \"http://ficcjira.xyz.com/rest/api/2/search?jql=project=ABC&fields=Timetracking\"").getInputStream();

BufferedReader input = new BufferedReader(new InputStreamReader(stream), 1);

input.readLine();

input.read();

input.read();

DataReader reader = new JsonReader(input)

.addField("TimeSpent", "//array/object/timespent")

.addRecordBreak("//array/object");

/*reader = new TransformingReader(reader)

.add(new BasicFieldTransformer("TimeSpent").stringToDouble());*/

DataWriter writer = new StreamWriter(System.out);

JobTemplate.DEFAULT.transfer(reader, writer);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

I get the following error: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified

解决方案

This is a horrible way to access the Jira Rest api. Since it is a REST API, you can call it in many ways. Some of them are:

I have some sample code for you here: https://github.com/somaiah/jrjc

Jersey: Use Jersey to call the REST APIs and read the responses.

If you are using Spring, you can use the RestTemplate. I have a small sample here: https://github.com/somaiah/restTemplate

Of course there is a whole slew of options for you- Restlet, Apache commons and so on.

You should use the curl option only if you are calling this from a bash script. In that case you can parse the response with jq (which I love) https://stedolan.github.io/jq/

你可能感兴趣的:(java,curl,json)