我怎样才能从Azure的物联网中心接收到的数据工作 [英] How can I work with the received DATA from Azure IoT Hub

查看:197
本文介绍了我怎样才能从Azure的物联网中心接收到的数据工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的数据:

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());
    }
}

在这里,我成为了产品名称和价格:

Here i become the product name and price:

System.out.println(String.format("| Message Payload: %s", new String(receivedEvent.getBody(), Charset.defaultCharset())));

我怎么能采取有效载荷,产品为String的产品;而价格为双价;

How can i take the Payload, product into a String product; and the price into double price;?

推荐答案

小飞侠和亚拉文帮我解决这个问题。

Peter Pan and Aravind helped me to solve the Problem.

下面是该问题的解决方案:

Here is the solution for the problem:

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字符串,产品类包含变量,产品价格UND吸气剂。它的工作原理!

Here i read the JSON String in into a Object from the Class Product, the Product class contains variables, product und price with getter. It works!

    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());
    }
}

这篇关于我怎样才能从Azure的物联网中心接收到的数据工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆