Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何处理从Azure IoT Hub接收到的数据_Java_Azure_Raspberry Pi_Iot_Azure Iot Hub - Fatal编程技术网

Java 我如何处理从Azure IoT Hub接收到的数据

Java 我如何处理从Azure IoT Hub接收到的数据,java,azure,raspberry-pi,iot,azure-iot-hub,Java,Azure,Raspberry Pi,Iot,Azure Iot Hub,我收到数据: public void accept(PartitionReceiver receiver) { System.out.println("** Created receiver on partition " + partitionId); try { while (true) { Iterable<EventData> receivedEvents = receiver.receive(10).get();

我收到数据:

public void accept(PartitionReceiver receiver)
{
    System.out.println("** Created receiver on partition " + partitionId);
    try {
        while (true) {
            Iterable<EventData> receivedEvents = receiver.receive(10).get();
            int batchSize = 0;
            if (receivedEvents != null)
            {
                for(EventData receivedEvent: receivedEvents)
                {                                    
                    System.out.println(String.format("| Time: %s", receivedEvent.getSystemProperties().getEnqueuedTime()));
                    System.out.println(String.format("| Device ID: %s", receivedEvent.getProperties().get("iothub-connection-device-id")));
                    System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));
                    batchSize++;
                }
            }
        }
    } catch (Exception e)
    {
        System.out.println("Failed to receive messages: " + e.getMessage());
    }
}

如何将有效负载、产品转换为字符串产品;把价格变成双倍价格

正如@Aravind所说,您可以定义一个POJO类来将数据打包为对象属性,如
有效载荷
,并使用一些json库(如)将数据序列化和反序列化为POJO和json字符串之间的事件体,或者从中选择一个最喜欢的对象。

Peter Pan和Aravind帮助我解决了这个问题

以下是问题的解决方案:

public class Product {
public Product(){

}
    private String product;
    private Double price;

public Product(String json ){
    Gson gson=new Gson();
    try{
        Product product =gson.fromJson(json, Product.class);
        if (product!=null){
            System.out.println("Name: " +product.getProduct());
        }
    }catch (Exception e){
        System.out.println("failed: " +e.getMessage());
    }
}

    public String getProduct() {
        return product;
    }

    public Double getPrice() {
        return price;
    }
 }
在这里,我将JSON字符串从Product类读入一个对象,Product类包含变量Product und price with getter。它起作用了

    public Product(String json ){
    Gson gson=new Gson();
    try{
        Product product =gson.fromJson(json, Product.class);
        if (product!=null){
            System.out.println("Name: " +product.getProduct());
        }
    }catch (Exception e){
        System.out.println("failed: " +e.getMessage());
    }
}

有效负载将是一个json字符串。因此,您可以反序列化到适当的对象并使用。。
    public Product(String json ){
    Gson gson=new Gson();
    try{
        Product product =gson.fromJson(json, Product.class);
        if (product!=null){
            System.out.println("Name: " +product.getProduct());
        }
    }catch (Exception e){
        System.out.println("failed: " +e.getMessage());
    }
}