简体   繁体   English

如何从URL获取数据并将其转换为json?

[英]How to get data from a URL and convert it into json?

I am trying to get data from a URL and store it in json format using the code below: 我正在尝试从URL获取数据,并使用以下代码将其存储为json格式:

String fullURL="http://XXX:8101/Myapp/XXX/XXX";

        URL u = new URL(fullURL);
            System.out.println(fullURL);
            HttpURLConnection huc = (HttpURLConnection) u.openConnection();
            System.out.println("Message :"+huc.getResponseMessage());
            JSONParser parser = new JSONParser();
            BufferedReader rd = new BufferedReader(new InputStreamReader(huc.getInputStream()));

            JSONArray a = (JSONArray) parser.parse(rd);

            for (Object o : a)
            {
                org.json.simple.JSONObject device = (org.json.simple.JSONObject) o;



                double kw = (double) device.get("value");
                System.out.println(kw);
                //getKw().setKw(kw);

                String sensortype = (String) device.get("senorType ");
                System.out.println(sensortype);
                //getSensorType().setSenorType(sensortype);

                Timestamp dateTime = (Timestamp) device.get("serverTimeStamp");
                System.out.println(dateTime);
                //getServerTimeStamp().setServerTimeStamp(dateTime);

  }

But I am getting the following error: 但是我收到以下错误:

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray.

What am I doing wrong and how to fix this? 我在做什么错以及如何解决这个问题?

i changed based on user comment finally my array is: 我根据用户评论更改了,最后我的数组是:

[
  {

    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "sensorType": "S"

  },
  {
    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "sensorType": "V"
  },
  {
    "value": 777,
    "percentage": 0,
    "serverTimeStamp": 1436900289000,
    "sensorType": "R"
  }
]

How come I am getting a java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double exception? 我怎么会收到java.lang.ClassCastException:无法将java.lang.Long强制转换为java.lang.Double异常?

device.get("value") is returning a Long value which cannot be case to Double . device.get("value")返回的Long值不能为Double大小写。

So change it to this: 因此,将其更改为:

long kw = (long) device.get("value");

After looking At your code, you are trying to cast value in double which is wrong ,you will get long instate of double when trying to get value of key "value" from JSONObject .To get your desire result first cast to long and then in double . 看你的代码后,你想在施放价值double这是wrong ,你会得到long的instate double试图获得的键值时"value"JSONObject 。为了得到你的愿望的结果首先转换为long ,然后在double

double kw = (double)((long) device.get("value"));

Other thing what I have observed is you are trying to cast value of key "serverTimeStamp" to Timestamp which is wrong ,instate you will get long the you have to create new Instance of Timestamp . 我观察到的另一件事是您正在尝试将键"serverTimeStamp""serverTimeStamp"Timestamp ,这是wrong ,因为您将"serverTimeStamp" long来创建新的Timestamp实例。

Timestamp dateTime = new Timestamp ((long) device.get("serverTimeStamp"));

Why not do it like this. 为什么不这样做呢。

 ((Number) device.get("value")).doubleValue();

You will never get Class cast exception if the returned instace is a number like int, long, float, double etc. 如果返回的实例是int,long,float,double等数字,则永远不会获得Class强制转换异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM