java - JSON.simple 库显示意外字符 (<)

标签 java json

Java 类

package voteHandler.Org.RSPSVote;
/* 
 * Author: Sieu Phan
 * Website: RSPS Vote
 * Data: 5/7/2014
 * Version: 1.1
 */
import java.io.IOException;
import java.net.URL; 
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;


public class voteHandler {
    private static String SERVER = "";
    private static String API_KEY = "";
    public voteHandler(String SERVER_TO_SET, String API_KEY_TO_SET)
    {
        SERVER = SERVER_TO_SET;
        API_KEY = API_KEY_TO_SET;
    }
    /*
        Sends a GET request to RSPS Vote API.
        RETURNS: a boolean
    */
    public static boolean authenticate(String AUTH)
    {
        String url = generateURL(AUTH);
        try {
            String getJSON = IOUtils.toString(new URL(url));
            JSONObject message = (JSONObject) JSONValue.parseWithException(getJSON);
            String messageData = (String) message.get("MESSAGE");
            System.out.println(messageData);
            int responseData = Integer.parseInt((String) message.get("RESPONSE"));
            System.out.println(responseData);              
            if(responseData == 5)
                return true;  
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        return false;           
    }   

    public static boolean getReward(String AUTH)
    {
        String url = generateURL(AUTH);
        return false;
    }
    /*
        Generates the url to the API 
        RETURNS: a String
    */
    private static String generateURL(String AUTH)
    {
        String URL = "http://rspsvote.com/widget/api.php?" 
                        + "action=check" 
                        + "&server=" + SERVER 
                        + "&key=" + API_KEY 
                        + "&auth=" + AUTH;
        return URL;
    }   
}

测试类

import voteHandler.Org.RSPSVote.voteHandler;


public class testing {
    public static void main(String args[])
    {
        voteHandler vote = new voteHandler("l6DZw", "ELWXQ-MI4BC-3K1RT-VUKPT-DGL2U");
        vote.authenticate("Ptpz1n");
    }
}

从网站返回的 JSON 格式有什么问题?

http://rspsvote.com/widget/api.php?key=ELWXQ-MI4BC-3K1RT-VUKPT-DGL2U&action=check&auth=Ptpz1n&server=l6DZw

这也是我从 eclipse 得到的错误:

Unexpected character (<) at position 63.
    at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
    at org.json.simple.JSONValue.parseWithException(JSONValue.java:78)
    at voteHandler.Org.RSPSVote.voteHandler.authenticate(voteHandler.java:34)
    at testing.main(testing.java:8)

最佳答案

what's wrong with this JSON format being returned from the site?

它不返回 JSON。响应的内容类型是 text/html 并且正文包含

{"RESPONSE":"7","MESSAGE":"AUTHENTICATION WAS UNSUCCESSFUL."}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

您需要删除 XML 或在 JSON 之后停止解析。

关于java - JSON.simple 库显示意外字符 (<),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24419748/

相关文章:

java - 来自 Java 的 HttpPost 不起作用

javascript - 我的网页无法通过 JSON 请求运行

java - Java如何将数组拆分为多个线程

java - 如何在使用 Hibernate 的 @Enumerated 时设置默认枚举值?

java - BACNet4J:如何获取和设置值?

javascript - 将 JS 变量名传递给函数

java - 构建时在 Eclipse 中自动安装 Maven 项目

java - 如何使用 apache POI 删除 xlsx 文件中的单元格内容

php - 从过程代码转向oop

json - 层次结构中存在循环!”json