用Java将网页响应解析为Json
我有一个java代码:用Java将网页响应解析为Json,java,json,Java,Json,我有一个java代码: URL oracle = new URL("https://x.x.x.x.x.x.-001"); System.out.println(oracle.openStream()); BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); String inputLine; while ((inputLine = in.readLine()) != null)
URL oracle = new URL("https://x.x.x.x.x.x.-001");
System.out.println(oracle.openStream());
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
正在打开连接并打印其内容。内容确实是Json。输出类似于:
{
"merchantId": "guest",
"txnId": "guest-1349269250-001",
}
我希望在简单的jar中解析它。我更改了代码循环,如下所示:
JSONObject obj = new JSONObject();
while ((inputLine = in.readLine()) != null)
obj.put("Result",inputLine);
但这似乎不起作用。我得到的结果是:
{"Result":"}"}
您应该首先读取字符串变量的全部内容,并将其解析为json。小心
”
(双引号)。Java使用\“
作为双引号
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonSimpleExample3 {
public static void main(String args[]) {
JSONParser parser = new JSONParser();
//String str = "{\"merchantId\": \"guest\",\"txnId\": \"guest-1349269250-001\",}";
//intilize an InputStream
InputStream is = new ByteArrayInputStream("file content".getBytes());
//read it with BufferedReader and create string
BufferedReader br = new BufferedReader(new InputStreamReader(is));// Instead of is, you should use oracle.openStream()
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e1) {
e1.printStackTrace();
}
// parse string
try {
JSONObject jsonObject = (JSONObject) parser.parse(sb.toString());
String merchantId = (String) jsonObject.get("merchantId");
System.out.println(merchantId);
String txnId = (String) jsonObject.get("txnId");
System.out.println(txnId);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
您应该首先读取字符串变量的全部内容并将其解析为json。请注意
“
(双引号)。Java使用\”
表示双引号。喜欢
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonSimpleExample3 {
public static void main(String args[]) {
JSONParser parser = new JSONParser();
//String str = "{\"merchantId\": \"guest\",\"txnId\": \"guest-1349269250-001\",}";
//intilize an InputStream
InputStream is = new ByteArrayInputStream("file content".getBytes());
//read it with BufferedReader and create string
BufferedReader br = new BufferedReader(new InputStreamReader(is));// Instead of is, you should use oracle.openStream()
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e1) {
e1.printStackTrace();
}
// parse string
try {
JSONObject jsonObject = (JSONObject) parser.parse(sb.toString());
String merchantId = (String) jsonObject.get("merchantId");
System.out.println(merchantId);
String txnId = (String) jsonObject.get("txnId");
System.out.println(txnId);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
您确定您正在学习如何解析JSON字符串吗
从外观上看,您必须获取整个字符串并在其上调用
JSONParse#parse()
,但是您的代码正在用JSON的每一行填充HashMap
(JSONObject
)的父类。事实上,它只存储最后一行,因为您在每次迭代中都使用相同的“Result”
键调用put()
。您确定要了解如何解析JSON字符串吗
从外观上看,您必须获取整个字符串并在其上调用JSONParse#parse()
,但是您的代码正在用JSON的每一行填充HashMap
(JSONObject
)的父类。事实上,它只存储最后一行,因为您在每次迭代中使用相同的“Result”
键调用put()
。您应该使用以下方法:
URL oracle = new URL("https://x.x.x.x.x.x.-001");
System.out.println(oracle.openStream());
Reader in = new InputStreamReader(oracle.openStream());
Object json = JSONValue.parse(in);
您应该使用以下方法:
URL oracle = new URL("https://x.x.x.x.x.x.-001");
System.out.println(oracle.openStream());
Reader in = new InputStreamReader(oracle.openStream());
Object json = JSONValue.parse(in);
试试这个链接,如果你要登录或者是这样的员工,它真的很有用
}试试这个链接,如果你打算登录或雇佣这样的员工,它真的很有用
}可能是
“txnId”:“guest-1349269250-001”
结尾的逗号给你带来了问题吗?你有例外吗?如果是这样,请发布堆栈跟踪。是否要将输出转换为json对象或读取输出??可能是“txnId”:“guest-1349269250-001”
结尾的逗号给您带来了问题?是否有异常?如果是这样,请发布堆栈跟踪。是否要将输出转换为json对象或读取输出??这是可行的,但假设我需要访问txnId
,执行json。txnId
会引发错误。您需要将json对象强制转换为适当的实例类。请看下面的示例。在您的例子中,JSONObject看起来是合适的:JSONObject json=(JSONObject)JSONValue.parse(In);之后,您可能可以编写json.get(“txnId”);访问txnId字段。这是可行的,但假设我需要访问txnId
,执行json。txnId
确实会抛出错误。您需要将json对象强制转换为适当的实例类。请看下面的示例。在您的例子中,JSONObject看起来是合适的:JSONObject json=(JSONObject)JSONValue.parse(In);之后,您可能可以编写json.get(“txnId”);访问txnId字段。这是可行的,但假设我需要访问txnId
,执行json。txnId
确实会引发错误这是可行的,但假设我需要访问txnId
,执行json。txnId
确实会引发错误