开发者

Extracting repeating attributes from one continuous string

I have a string with has two types of separator.

String X = "20001=EDTS~^20002=USA~^20003=1170871875~^20004=1~^20005=0~^773=~^665=~^453=2~^448=0A~!447=D~!452=1~!~^448=0A~!447=D~!452=17~!~^11=001111652533408~^";

~^ denotes lone values

where ~! denotes groups of values.

448=0A~!447=D~!452=1~!~^448=0A~!447=D~!452=17~!~^

previously I was working off the assumption there was no repeating groups of data and simply mapping to a map based on the key being equal to the tag number. However this will not for for the repeating groups as they will overwrite.

EDIT I want to take values from this string and map them to a Different Objects attributes. basically the number to the left refers to a tag and the value is just the value. So tag 20002=USA, I will need to map the value U开发者_运维问答SA to an attribute within my new object. Invalid values are just defaulted values so that flag up that they have been assigned but not got a value

Now I'm doing a bit of head scratching to think of a better way to map the data and with the groups. maintaining both the tag and the value associated with it.


Eek... bad data format. JSON would be a much prettier. URL encoded parameter strings would better too. Both would allow for arbitrary nesting of groups if that ever happens.

But perhaps something like this?

String groupDelim="~!";
String entryDelim="~^";
String pairDelim="=";

String[] groups = X.split(groupDelim);
int groupId = 0;
for(String group:groups){
   String entries = group.split(entryDelim);
   int entryId = 0;
   for(String entry:entries){
      String[] pair = entry.split(pairDelim);
      /* now do stuff with */
      groupId; // for sort order if it matters
      entryId; // for sort order if it matters
      String key = pair[0];
      String value = pair[1]; 
      entryId++;
   }
   groupId++;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜