简体   繁体   English

如何从同一字段中具有不同类的JSON中读取数字?

[英]How to read numbers from JSON with different class in the same field?

I am trying to extract "sentiment"value from JSON file. 我正在尝试从JSON文件中提取“情感”值。

{
  "id": 1140,
  "company": "Barclays",
  "title": "Barclays set to name former JPMorgan banker Staley as new CEO",
  "sentiment": 0.000
},
{
  "id": 1141,
  "company": "Kingfisher",
  "title": "Kingfisher share price slides on cost to implement new strategy",
  "sentiment": -0.786
},

If I am not mistaken, "-0.786" is double while "0.000" is a long.The first few output is success until : 如果我没记错的话,“-0.786”是双精度字符,而“ 0.000”是一个长整数。前几个输出是成功的,直到:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double

I am very new in this, is there any method to solve this or anything to refer to? 我对此很陌生,是否有解决此问题的方法或需要参考的内容?

Code: 码:

public static void printScore(String data)
{
    JSONParser parser = new JSONParser();
    String link = data;

    try {     
        JSONArray a = (JSONArray) parser.parse(new FileReader(link));

        for (Object o:a){
            JSONObject jsonObject =  (JSONObject) o;

            double score = (double) jsonObject.get("sentiment");
            //DecimalFormat df = new DecimalFormat(#.###);
            System.out.format("%.3f",score);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Try this code: 试试这个代码:

package com.test;

import java.io.FileNotFoundException;
import java.io.FileReader;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class Json {

    public static void main(String[] args) {
        printScore("test.json");
    }

    public static void printScore(String data) {
        JsonParser parser = new JsonParser();
        String link = data;
        JsonArray a;
        try {
            a = (JsonArray) parser.parse(new FileReader(link));
            for (Object o : a) {
                JsonObject jsonObject = (JsonObject) o;
                double score = jsonObject.get("sentiment").getAsDouble();
                System.out.format("%.3f", score);
            }
        } catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

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

相关问题 如何从 Java 中的不同类读取私有字段的值? - How to read the value of a private field from a different class in Java? 如何从具有不同字段的同一类创建两个 json 文件 - How to create the two json file from same class with different fields 如何从只读基础 class 覆盖 JSON 字段并改用子字段? - How to override JSON field from read-only base class and use child field instead? 从不同的类生成2个数字时如何停止获取相同的数字 - how to stop getting the same number when generating 2 numbers from a different class 是否有可能从不同 class 的同一端口上的套接字写入/读取 - Is there a possiblity to write/read from a socket on same port from different class 如何使用Gson解析相同字段但不同类型的JSON - how to use Gson to resolve the same field but different types of JSON 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? 同一类的不同JSON序列化 - Different JSON serialization for the same class 如何从不同的罐子加载相同的类 - how to load same class from different jars 如何使用不同类别的同一个Webdriver - How to use the same Webdriver from different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM