0

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([])?

3
  • 1
    That isn't json - why do you want to use a json serializer? It would be better to simply tell us what the rules are for that format, so we can offer guidance. But my guidance: forget json - that isn't json. Commented Feb 13, 2011 at 9:07
  • The format looks just like delimited key-value pairs, but it is hard to see what you want that to look like as an object... Do you have an example interpretation? i.e. What does that data represent? Commented Feb 13, 2011 at 9:10
  • tanks for your comments. these pair-value are output of a gprs data logger that sent via sms. considering the structure of string is not consistent (i.e some pair/values are omitted some times), what is the best way to read such a string to object (deserialize)? tanks Commented Feb 13, 2011 at 19:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

tanks for your reply, assume that i have a class with STA, L, TM , D ,... properties. and i know some times in above string some of pair values are omitted, know what is the best practice to read such a string to class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.