.net 把下面字符串数据的首尾去除空格 并 转换为json数组
时间: 2024-02-16 10:04:31 浏览: 70
假设字符串数据为:
```csharp
string data = " [ 1, 2 , 3 ,4] ";
```
可以使用以下代码实现要求:
```csharp
using System;
using System.Text.Json;
class Program
{
static void Main(string[] args)
{
string data = " [ 1, 2 , 3 ,4] ";
data = data.Trim(); // 去除首尾空格
var jsonArray = JsonSerializer.Deserialize<int[]>(data); // 转换为 int 数组
string json = JsonSerializer.Serialize(jsonArray); // 转换为 json 字符串
Console.WriteLine(json);
}
}
```
输出结果为:
```json
[1,2,3,4]
```
相关问题
.net 把下面字符串数据的首尾去除空格 并 转换为json数组 { "ScraperData":{ "AveFuelConsum":0, "BucketCount":0, "DownTime":0, "PowerLoadRate":0, "TorqueLoadRate":0, "TotalFuelConsum":0, "TotalMileage":0, "WeightPerBucket":0, "WorkingTime":0, "WorkingWeights":0 }, "VehicleDisplay":{ "AcceleratorPedal":0, "BrakeAcPressure":0, "BrakeOilTemperature":0, "ControlStatus":true, "DoorStatus":false, "EmergencyStop":0, "EngineOilPressure":0, "EngineSpeed":0, "EngineWaterTem":0, "ForwardOrReverse":false, "FrontBrakePressure":0, "FrontLeft":0, "FrontRight":0, "FuelLevel":0, "GearPosition":0, "HighBeam":0, "LowBeam":0, "ManualOAautStatus":0, "OilTemperature":0, "ParkBrakeStatus":0, "RearBrakePressure":0, "RearLeft":0, "RearRight":0, "ShiftPressure":0, "TorqueTemperature":0, "VehicleSpeed":0, "ZHBrakePressure":0 } }
假设字符串数据为:
```csharp
string data = @" {""ScraperData"":{ ""AveFuelConsum"":0, ""BucketCount"":0, ""DownTime"":0, ""PowerLoadRate"":0, ""TorqueLoadRate"":0, ""TotalFuelConsum"":0, ""TotalMileage"":0, ""WeightPerBucket"":0, ""WorkingTime"":0, ""WorkingWeights"":0 }, ""VehicleDisplay"":{ ""AcceleratorPedal"":0, ""BrakeAcPressure"":0, ""BrakeOilTemperature"":0, ""ControlStatus"":true, ""DoorStatus"":false, ""EmergencyStop"":0, ""EngineOilPressure"":0, ""EngineSpeed"":0, ""EngineWaterTem"":0, ""ForwardOrReverse"":false, ""FrontBrakePressure"":0, ""FrontLeft"":0, ""FrontRight"":0, ""FuelLevel"":0, ""GearPosition"":0, ""HighBeam"":0, ""LowBeam"":0, ""ManualOAautStatus"":0, ""OilTemperature"":0, ""ParkBrakeStatus"":0, ""RearBrakePressure"":0, ""RearLeft"":0, ""RearRight"":0, ""ShiftPressure"":0, ""TorqueTemperature"":0, ""VehicleSpeed"":0, ""ZHBrakePressure"":0 } }";
```
可以使用以下代码实现要求:
```csharp
using System;
using System.Text.Json;
class Program
{
static void Main(string[] args)
{
string data = @"{""ScraperData"":{ ""AveFuelConsum"":0, ""BucketCount"":0, ""DownTime"":0, ""PowerLoadRate"":0, ""TorqueLoadRate"":0, ""TotalFuelConsum"":0, ""TotalMileage"":0, ""WeightPerBucket"":0, ""WorkingTime"":0, ""WorkingWeights"":0 }, ""VehicleDisplay"":{ ""AcceleratorPedal"":0, ""BrakeAcPressure"":0, ""BrakeOilTemperature"":0, ""ControlStatus"":true, ""DoorStatus"":false, ""EmergencyStop"":0, ""EngineOilPressure"":0, ""EngineSpeed"":0, ""EngineWaterTem"":0, ""ForwardOrReverse"":false, ""FrontBrakePressure"":0, ""FrontLeft"":0, ""FrontRight"":0, ""FuelLevel"":0, ""GearPosition"":0, ""HighBeam"":0, ""LowBeam"":0, ""ManualOAautStatus"":0, ""OilTemperature"":0, ""ParkBrakeStatus"":0, ""RearBrakePressure"":0, ""RearLeft"":0, ""RearRight"":0, ""ShiftPressure"":0, ""TorqueTemperature"":0, ""VehicleSpeed"":0, ""ZHBrakePressure"":0 } }";
data = data.Trim(); // 去除首尾空格
var jsonObject = JsonSerializer.Deserialize<JsonElement>(data); // 转换为 JsonElement 对象
string json = JsonSerializer.Serialize(jsonObject); // 转换为 json 字符串
Console.WriteLine(json);
}
}
```
输出结果为:
```json
{"ScraperData":{"AveFuelConsum":0,"BucketCount":0,"DownTime":0,"PowerLoadRate":0,"TorqueLoadRate":0,"TotalFuelConsum":0,"TotalMileage":0,"WeightPerBucket":0,"WorkingTime":0,"WorkingWeights":0},"VehicleDisplay":{"AcceleratorPedal":0,"BrakeAcPressure":0,"BrakeOilTemperature":0,"ControlStatus":true,"DoorStatus":false,"EmergencyStop":0,"EngineOilPressure":0,"EngineSpeed":0,"EngineWaterTem":0,"ForwardOrReverse":false,"FrontBrakePressure":0,"FrontLeft":0,"FrontRight":0,"FuelLevel":0,"GearPosition":0,"HighBeam":0,"LowBeam":0,"ManualOAautStatus":0,"OilTemperature":0,"ParkBrakeStatus":0,"RearBrakePressure":0,"RearLeft":0,"RearRight":0,"ShiftPressure":0,"TorqueTemperature":0,"VehicleSpeed":0,"ZHBrakePressure":0}}
```
json空的字符串怎么判断
判断一个 JSON 字符串是否为空,可以通过以下两种方法:
1. 使用 Python 内置的 json 库进行解析,然后判断解析后的结果是否为空:
```python
import json
json_str = '{"name": "John", "age": 30, "city": "New York"}'
parsed_json = json.loads(json_str)
if not parsed_json:
print("JSON 字符串为空")
```
2. 直接判断字符串本身是否为空:
```python
json_str = '{}'
if not json_str.strip():
print("JSON 字符串为空")
```
其中,`strip()` 方法可以去除字符串首尾的空白字符,包括空格、制表符、换行符等。
阅读全文