JSON解析器:避免元数据

时间:2016-09-28 13:58:04

标签: java json

我有简单的json解析器。我是新手,所以请不要以严厉的方式回答......

这是我的代码:

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

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;
import org.json.simple.parser.JSONParser;

public class ParseJsonFaculties
{
    public static void main(String[] args) {
        String url = "https://somesite/";
        JSONParser parser = new JSONParser();

    try {

        Object obj = parser.parse(url);

        JSONObject jsonObject = (JSONObject) obj;

                JSONArray id = (JSONArray) jsonObject.get("Id");
                Iterator<String> iteratorId = id.iterator();
                while (iteratorId.hasNext()) {
                        System.out.println(iteratorId.next());
                }

                JSONArray name = (JSONArray) jsonObject.get("Name");
                Iterator<String> iteratorName = name.iterator();
                while (iteratorName.hasNext()) {
                        System.out.println(iteratorName.next());
                }

        JSONArray sname = (JSONArray) jsonObject.get("ShortName");
        Iterator<String> iteratorSname = sname.iterator();
        while (iteratorSname.hasNext()) {
            System.out.println(iteratorSname.next());
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }
    }
}

运行后我得到了这个错误:

org.json.simple.parser.ParseException: Unexpected character (h) at position 0.

正如我认为的那样,由于来自服务器的json响应,这实际上看起来像是:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 4559
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 27 Sep 2016 08:53:40 GMT

[{json here}]

如果是这种情况,如何避免元数据?

感谢。

=====

通过重写代码来解决:

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class UniverisFacultiesReciever {
    public static void main(String[] args) {
        String urlString = "https://somesite/";
        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String result = "";
            String output = "";
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                result = result + output;
            }

            conn.disconnect();


            //start parse.

        JSONArray jsonarray = new JSONArray(result);

        for(int i=0; i<jsonarray.length(); i++){

        JSONObject obj = jsonarray.getJSONObject(i);

        String id = obj.getString("Id");
        String name = obj.getString("Name");
        String shortname = obj.getString("ShortName");
        System.out.println("Faculty ID: ");
        System.out.print(id);
        System.out.println("Faculty name: ");
        System.out.print(name);
        System.out.println("Faculty short name: ");
        System.out.print(shortname);
        }

    } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (JSONException e) {

        e.printStackTrace();
    }
    }
}

很抱歉这么糟糕的代码,它仍然是WIP。

0 个答案:

没有答案