开发者

custom serialization/deserialization using JSON .net

My application receives SMS in following format:

STA:000000,000;L:310;TM:090516195102;D:1;T:01;C:25;A00:0.166;A01:00000;A02:0.578;A03:00 000;A04:00000;A05:00000;A06:00000;A07:00000;A08:00000;A09:00000;A10:00000;A11:00000;A1 2:00000;A13:31.00;A14:30.93;P01:00000000;P02:00000000;P03:00000000;P04:00000000;P05:000 00000;P06:00000000;K01:13333330000000000;O01:0000;8F

I want to deserialize this string to an object. I already have read about JSON tool, but I don't know if I serialize and deserialize in this format. That is, can I change the default delimiter(,) and class({}) and array notations(开发者_运维百科[])?


This looks like a ; separated list of key-value pairs to me, where key and value are separated by :. The following code parses is as such. I don't see what this has to do with JSON.

const string testInput="STA:000000,000;L:310;TM:090516195102;D:1;T:01;C:25;A00:0.166;A01:00000;A02:0.578;A03:00"+
        "000;A04:00000;A05:00000;A06:00000;A07:00000;A08:00000;A09:00000;A10:00000;A11:00000;A1"+
        "2:00000;A13:31.00;A14:30.93;P01:00000000;P02:00000000;P03:00000000;P04:00000000;P05:000"+
        "00000;P06:00000000;K01:13333330000000000;O01:0000";

IEnumerable<KeyValuePair<string,string>> ParseList(string input)
{
    string[] lines=input.Split(';');
    foreach(string line in lines)
    {
        string[] parts=line.Split(':');
        if(parts.Length!=2)
            throw new InvalidDataException(line);
        yield return new KeyValuePair<string,string>(parts[0],parts[1]);
    }
}

void Main()
{
     ParseList(testInput).Dump();       
}

It doesn't handle the last ;8F but I assume that's only an artifact of a truncated message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜