首页 新闻 会员 周边 捐助

使用c#改动这个json字符串中的值

0
悬赏园豆:50 [已解决问题] 解决于 2015-08-02 20:09

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字符串,这四个值是动态添加或减少,不是替换

c#
尼哈的主页 尼哈 | 初学一级 | 园豆:38
提问于:2015-02-28 11:14
< >
分享
最佳答案
0

简单的方法是反序列化后通过对象属性修改,然后再序列化。

收获园豆:25
Launcher | 高人七级 |园豆:45050 | 2015-02-28 11:18

这个字符串反序列化通过对象改变属性值,在序列化,占用时间比较久呢,这个字符串修改要求时间很短

尼哈 | 园豆:38 (初学一级) | 2015-02-28 11:24

@尼哈: 如果你考虑这个问题的话,那么你可以自己写段代码,通过遍历此字符串来修改。

Launcher | 园豆:45050 (高人七级) | 2015-02-28 11:30

@Launcher: 我通过遍历修改在反序列化,感觉速度还是很慢

尼哈 | 园豆:38 (初学一级) | 2015-02-28 13:14

@尼哈: 你用的是字符串遍历修改(这里已经不涉及到序列化和反序列化的问题),怎么还需要反序列化呢?你能把你的问题讲清楚点吗?

Launcher | 园豆:45050 (高人七级) | 2015-02-28 13:21

@Launcher: 我就是需要改动  每个对象中adultTax与 duration的值,然后json字符串原样输出

尼哈 | 园豆:38 (初学一级) | 2015-02-28 13:28

@尼哈: 你是不是不会写直接遍历字符串实现解析的算法?

Launcher | 园豆:45050 (高人七级) | 2015-02-28 13:35

@Launcher: 遍历解析弄好了,但是不知道怎么改变值,在封装成字符串,感觉按我的思路,效率会很低

尼哈 | 园豆:38 (初学一级) | 2015-02-28 13:37

@尼哈: 我给你大概描述下算法:

1、StringBuilder output ,用于保存结果。

2、每次从源 json 字符串中读取一个字符,如果是 ["] ,并且前一次读取的符号是 [,] 或者 [{],则开始执行 3,否则将读取到的字符写入到 output 中去。

3、连续读入多个字符,直到读入字符为 ["],然后判断读入的字符串是否为 adultTax 或 duration,如果是则执行 4,否则将读入的字符写入 output;

4、连续读入多个字符,直到读入字符为 [,],舍弃此次读入的字符串,把新的值直接写入到 output,然后回到 2。

Launcher | 园豆:45050 (高人七级) | 2015-02-28 14:01

@Launcher: 厉害

尼哈 | 园豆:38 (初学一级) | 2015-02-28 15:09
其他回答(3)
0

正则式替换效率最高,反序列化在序列化可控性高

收获园豆:10
刘宏玺 | 园豆:14020 (专家六级) | 2015-02-28 11:25

正则替换这个表达式感觉很难写

支持(0) 反对(0) 尼哈 | 园豆:38 (初学一级) | 2015-02-28 13:48

@尼哈: 楼下都给你写出来了

支持(0) 反对(0) 刘宏玺 | 园豆:14020 (专家六级) | 2015-02-28 13:49

@刘宏玺: 那个我试了获取空值

支持(0) 反对(0) 尼哈 | 园豆:38 (初学一级) | 2015-02-28 13:54
0

感觉还是转对象方便点。Json.NET速度不慢的吧。

收获园豆:5
DZCMS | 园豆:5 (初学一级) | 2015-02-28 16:26
0

遍历替换 没保障 处理起来麻烦 使用Json.Net 是不错的选择 

收获园豆:5
Chaoa | 园豆:643 (小虾三级) | 2015-03-11 11:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册