Java 从json输出中检索精确文本
我使用的是非正式的ginger语法检查API,它以json格式返回结果。我想提取它在作为输入提供的文本中给出的更正 我已经这样做了,但它只作为静态工作,也就是说,我必须手动调整代码,然后返回输出 下面是我的代码-Java 从json输出中检索精确文本,java,json,Java,Json,我使用的是非正式的ginger语法检查API,它以json格式返回结果。我想提取它在作为输入提供的文本中给出的更正 我已经这样做了,但它只作为静态工作,也就是说,我必须手动调整代码,然后返回输出 下面是我的代码- import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONString;
//import org.json.simple.JSONObject;
import org.json.JSONObject;
import com.jayway.jsonpath.JsonPath;
import org.json.simple.parser.*;
public class Demo {
static class urllib {
public static StringBuffer urlopen(URL url) {
try {
//URL u = new URL(url);
StringBuffer sb = new StringBuffer();
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String str = null;
while((str = in.readLine())!=null) {
sb.append(str);
}
in.close();
return sb;
} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
}
public static void main(String[] args) {
StringBuffer result = null;
String finalResult = null;
StringBuffer response;
String finalResponse = null;
String finalText=null;
try{
//connections settings
String data = new String("a apple");
finalText=data;
String s[]=data.split(" ");
int i=0;
String text =s[0] ;
for(i=1;i<s.length;i++)
{
text=text+"+"+s[i];
}
System.out.println(text);
String Finaldata= new String(""+text);
URL url = new URL("http://services.gingersoftware.com/Ginger/correct/json/GingerTheText?lang=US&clientVersion=2.0&apiKey=XXXXXX-XXXX-XXXX-XXXX&text="+Finaldata);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput (true);
con.setRequestMethod("POST");
System.out.println("connection done");
JSONObject json ;
JSONParser parser = new JSONParser();
try
{
response =urllib.urlopen(url);
finalResponse= response.toString();
//System.out.println("final response"+finalResponse);
json = new JSONObject(finalResponse);
//System.out.println(json);
finalResult=json.toString();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
//This stores corrected text
String temp = new JSONObject(finalResult)
.getJSONArray("LightGingerTheTextResult").getJSONObject(0)
.getJSONArray("Suggestions").getJSONObject(0)
.getString("Text");
System.out.println(temp);
//This stores which part is corrected
String temp1 = new JSONObject(finalResult)
.getJSONArray("LightGingerTheTextResult").getJSONObject(0)
.getJSONArray("Suggestions").getJSONObject(1)
.getString("Text");
System.out.println(temp1);
temp=temp.toLowerCase();
temp1=temp1.toLowerCase();
String newString= finalText.replace(temp1, temp);
System.out.println(newString);
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.urlcoder;
导入java.util.List;
导入java.util.Map;
导入java.net.URL;
导入org.json.JSONArray;
导入org.json.JSONString;
//导入org.json.simple.JSONObject;
导入org.json.JSONObject;
导入com.jayway.jsonpath.jsonpath;
导入org.json.simple.parser.*;
公开课演示{
静态类urllib{
公共静态StringBuffer URL打开(URL URL){
试一试{
//URL u=新的URL(URL);
StringBuffer sb=新的StringBuffer();
BufferedReader in=新的BufferedReader(
新的InputStreamReader(url.openStream());
字符串str=null;
而((str=in.readLine())!=null){
某人附加(str);
}
in.close();
归还某人;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
}
公共静态void main(字符串[]args){
StringBuffer结果=null;
字符串finalResult=null;
字符串缓冲响应;
字符串finalResponse=null;
字符串finalText=null;
试试{
//连接设置
字符串数据=新字符串(“苹果”);
finalText=数据;
字符串s[]=data.split(“”);
int i=0;
字符串文本=s[0];
对于(i=1;iIs在选项上使用gson?我对此没有任何明确的想法。-研究如何使用fromJson()
方法能否将这一行**response=urllib.urlopen(url);**替换为静态JSON以便我们可以运行它?