You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Retrofit提取JSON指定字段并优化输出格式?

Fixing Redundant JSON Formatting for Sensor Data Output

Got it, let's sort out that annoying {"":""} formatting showing up in your output. The issue is probably that you're printing the entire parsed JSON object or your API response class directly (which uses the default toString() method that spits out JSON-style formatting). Instead, we need to explicitly extract each sensor's value and format the output exactly how you want.

Step 1: Define a Proper Response Class

First, let's make sure you have a Java class that maps correctly to your sensor JSON structure. Assuming your incoming JSON looks like this (super common for sensor APIs):

{
  "data": {
    "1": 24,
    "2": 68,
    "3": 1023,
    "4": 512
  }
}

Create a nested class to capture the sensor values, using annotations to map those numeric keys to readable fields:

Using Jackson (Most Popular for Java APIs)

import com.fasterxml.jackson.annotation.JsonProperty;

public class SensorApiResponse {
    private SensorData data;

    // Getters and Setters
    public SensorData getData() { return data; }
    public void setData(SensorData data) { this.data = data; }

    public static class SensorData {
        @JsonProperty("1")
        private int temperatureSensor; // Rename to whatever makes sense for your sensor 1
        @JsonProperty("2")
        private int humiditySensor;    // Sensor 2
        @JsonProperty("3")
        private int pressureSensor;    // Sensor 3
        @JsonProperty("4")
        private int motionSensor;      // Sensor 4

        // Getters for each sensor
        public int getTemperatureSensor() { return temperatureSensor; }
        public int getHumiditySensor() { return humiditySensor; }
        public int getPressureSensor() { return pressureSensor; }
        public int getMotionSensor() { return motionSensor; }
    }
}

Using Gson (Alternative JSON Library)

If you're using Gson instead, use @SerializedName instead of @JsonProperty:

import com.google.gson.annotations.SerializedName;

public class SensorApiResponse {
    private SensorData data;

    // Getters and Setters
    public SensorData getData() { return data; }
    public void setData(SensorData data) { this.data = data; }

    public static class SensorData {
        @SerializedName("1")
        private int temperatureSensor;
        @SerializedName("2")
        private int humiditySensor;
        @SerializedName("3")
        private int pressureSensor;
        @SerializedName("4")
        private int motionSensor;

        // Getters here
    }
}

Step 2: Parse and Print Cleanly

In your main class where you call the API, instead of printing the entire SensorApiResponse object, extract each sensor value and format your output:

// Example with Jackson
ObjectMapper mapper = new ObjectMapper();
SensorApiResponse response = mapper.readValue(yourApiJsonString, SensorApiResponse.class);

// Now print exactly what you want, no redundant JSON
System.out.println("Temperature (Sensor 1): " + response.getData().getTemperatureSensor());
System.out.println("Humidity (Sensor 2): " + response.getData().getHumiditySensor());
System.out.println("Pressure (Sensor 3): " + response.getData().getPressureSensor());
System.out.println("Motion (Sensor 4): " + response.getData().getMotionSensor());

If Your JSON Data is an Array

If your data field is a simple array like {"data": [24, 68, 1023, 512]}, simplify your response class:

public class SensorApiResponse {
    private int[] data;

    public int[] getData() { return data; }
    public void setData(int[] data) { this.data = data; }
}

Then print like this:

SensorApiResponse response = mapper.readValue(yourApiJsonString, SensorApiResponse.class);
System.out.println("Sensor 1: " + response.getData()[0]);
System.out.println("Sensor 2: " + response.getData()[1]);
System.out.println("Sensor 3: " + response.getData()[2]);
System.out.println("Sensor 4: " + response.getData()[3]);

Why This Works

Previously, you were likely doing something like System.out.println(response) which calls the default toString() method of your response class (or the parsed JSON object), which outputs the full JSON structure. By explicitly accessing each sensor's value and building your own output string, you eliminate that redundant formatting entirely.

内容的提问来源于stack exchange,提问作者Rikhil Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:15:31