gpt4 book ai didi

json - 如何从 java 客户端使用 JSON Webservice?

转载 作者:行者123 更新时间:2023-12-02 00:14:31 24 4
gpt4 key购买 nike

public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {

Map countryList = new HashMap();

String str = "http://10.10.10.25/TEPortalIntegration/CustomerPortalAppIntegrationService.svc/PaymentSchedule/PEPL/Unit336";

try {
URL url = new URL(str);

URLConnection urlc = url.openConnection();

BufferedReader bfr = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

String line, title, des;

while ((line = bfr.readLine()) != null) {

JSONArray jsa = new JSONArray(line);

for (int i = 0; i < jsa.length(); i++) {
JSONObject jo = (JSONObject) jsa.get(i);

title = jo.getString("Amount");

countryList.put(i, title);
}

renderRequest.setAttribute("out-string", countryList);

super.doView(renderRequest, renderResponse);
}
} catch (Exception e) {

}
}

我正在尝试从 liferay portlet 类访问 json 对象,我想将任何 json 字段的值数组传递给 jsp 页面。

最佳答案

在将其转换为 JSON 数组之前,您需要阅读完整的响应。这是因为响应中的每一行都将是一个(无效的)JSON 片段,无法单独解析。稍作修改后,您的代码应该可以正常工作,突出显示如下:

// fully read response
final String line;
final StringBuilder builder = new StringBuilder(2048);

while ((line = bfr.readLine()) != null) {
builder.append(line);
}

// convert response to JSON array
final JSONArray jsa = new JSONArray(builder.toString());

// extract out data of interest
for (int i = 0; i < jsa.length(); i++) {
final JSONObject jo = (JSONObject) jsa.get(i);
final String title = jo.getString("Amount");

countryList.put(i, title);
}

关于json - 如何从 java 客户端使用 JSON Webservice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com