json字符串如下:
{
"airTicketListResponse": {
"currency": "CNY",
"customerSessionId": "S12345",
"routings": [
{
"totalPrice": "4479.00",
"key": "UUzoHUQ==",
"mainAirlineCode": "AC",
"gds": "S",
"adultTax": "1310.64",
"duration": 1063,
"overnightStay": false,
"totalSalesPrice": "4479.00",
"adultBasePrice": "3168.00",
"childTax": "0",
"childBasePrice": "0",
"mainAirlineName": "Air Canada",
"maxSeats": 1,
"layover": true,
"publishFare": true,
"adultSalesPrice": "3168.00",
"majorAirline": false,
"childSalesPrice": "0",
"domesticTicket": false,
"operatedBy": false,
"overnightFlight": true,
"lastTicketDate": "02/27/2015",
"trips": [
{
"duration": 1063,
"tripTime": 1940
}
],
"tripTime": 1940
},
{
"totalPrice": "5600.00",
"key": "0JIbgiA==",
"mainAirlineCode": "CX",
"gds": "S",
"adultTax": "532.13",
"duration": 1165,
"overnightStay": false,
"totalSalesPrice": "5600.00",
"adultBasePrice": "5067.00",
"childTax": "0",
"childBasePrice": "0",
"mainAirlineName": "Cathay Pacific",
"maxSeats": 1,
"layover": false,
"publishFare": false,
"adultSalesPrice": "5067.00",
"majorAirline": false,
"childSalesPrice": "0",
"domesticTicket": false,
"operatedBy": false,
"overnightFlight": true,
"lastTicketDate": "05/19/2015",
"trips": [
{
"duration": 1165,
"tripTime": 1345
}
],
"tripTime": 1345
},
{
"totalPrice": "5600.00",
"key": "0JIbgiA==",
"mainAirlineCode": "CX",
"gds": "S",
"adultTax": "532.13",
"duration": 1165,
"overnightStay": false,
"totalSalesPrice": "5600.00",
"adultBasePrice": "5067.00",
"childTax": "0",
"childBasePrice": "0",
"mainAirlineName": "Cathay Pacific",
"maxSeats": 1,
"layover": false,
"publishFare": false,
"adultSalesPrice": "5067.00",
"majorAirline": false,
"childSalesPrice": "0",
"domesticTicket": false,
"operatedBy": false,
"overnightFlight": true,
"lastTicketDate": "05/19/2015",
"trips": [
{
"duration": 1165,
"tripTime": 1345
}
],
"tripTime": 1345
},
{
"totalPrice": "6003.00",
"key": "BhWJQ==",
"mainAirlineCode": "AC",
"gds": "W",
"adultTax": "1310.64",
"duration": 890,
"overnightStay": false,
"totalSalesPrice": "6003.00",
"adultBasePrice": "4692.00",
"childTax": "0",
"childBasePrice": "0",
"mainAirlineName": "Air Canada",
"maxSeats": 7,
"layover": false,
"publishFare": false,
"adultSalesPrice": "4692.00",
"majorAirline": false,
"childSalesPrice": "0",
"domesticTicket": false,
"operatedBy": true,
"overnightFlight": true,
"lastTicketDate": "03/02/2015",
"trips": [
{
"duration": 890,
"tripTime": 1165
}
],
"tripTime": 1165
}
]
}
}请问如何快速改变这个json里面字符串 中totalPrice, totalSalesPrice , adultBasePrice ,adultSalesPrice的值然后输出这个json字符串,这四个值是动态添加或减少,不是替换
简单的方法是反序列化后通过对象属性修改,然后再序列化。
这个字符串反序列化通过对象改变属性值,在序列化,占用时间比较久呢,这个字符串修改要求时间很短
@尼哈: 如果你考虑这个问题的话,那么你可以自己写段代码,通过遍历此字符串来修改。
@Launcher: 我通过遍历修改在反序列化,感觉速度还是很慢
@尼哈: 你用的是字符串遍历修改(这里已经不涉及到序列化和反序列化的问题),怎么还需要反序列化呢?你能把你的问题讲清楚点吗?
@Launcher: 我就是需要改动 每个对象中adultTax与 duration的值,然后json字符串原样输出
@尼哈: 你是不是不会写直接遍历字符串实现解析的算法?
@Launcher: 遍历解析弄好了,但是不知道怎么改变值,在封装成字符串,感觉按我的思路,效率会很低
@尼哈: 我给你大概描述下算法:
1、StringBuilder output ,用于保存结果。
2、每次从源 json 字符串中读取一个字符,如果是 ["] ,并且前一次读取的符号是 [,] 或者 [{],则开始执行 3,否则将读取到的字符写入到 output 中去。
3、连续读入多个字符,直到读入字符为 ["],然后判断读入的字符串是否为 adultTax 或 duration,如果是则执行 4,否则将读入的字符写入 output;
4、连续读入多个字符,直到读入字符为 [,],舍弃此次读入的字符串,把新的值直接写入到 output,然后回到 2。
@Launcher: 厉害
正则式替换效率最高,反序列化在序列化可控性高
正则替换这个表达式感觉很难写
@尼哈: 楼下都给你写出来了
@刘宏玺: 那个我试了获取空值
感觉还是转对象方便点。Json.NET速度不慢的吧。
遍历替换 没保障 处理起来麻烦 使用Json.Net 是不错的选择