java parsing of complex string? -


i getting string response server this..

[  anchor{anchorname='&loreal_lip_balm',     clientname='loreal india',    anchorprice=10,         campaigns=[                 campaign{                         campaignid='loreal_camp1',                          question='how turn melted lipstick tinted lip balm',                          startdate=2015-08-04,                         enddate=2015-08-04,                         imageurl='null',                          regioncountry='all',                         rewardinfo='null',                         campaignprice=20,                          options=null               }       ]},     anchor{              anchorname='&loreal_total_repair_5',             clientname='loreal india',             anchorprice=125,            campaigns=[                     campaign{                     campaignid='loreal_camp2',                             question='is                          product buy?',                             startdate=2015-08-04,                             enddate=2015-08-04,                           imageurl='null',                             regioncountry='all',                             rewardinfo='null',                           campaignprice=20,                             options=null                     }                 ]        }  ]. 

can tell me how parse this.it not json response.

i used hand coded parsers simple languages felt using http://www.antlr.org/ or https://javacc.java.net bit heavy. used same structure parsing json, css simple template. modified parse response. see whether helps.

package snippet;  import java.io.ioexception; import java.io.pushbackreader; import java.io.reader; import java.io.stringreader; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.arrays; import java.util.hashmap; import java.util.list; import java.util.map;  import snippet.recursivedescentparser.token.tokentype;  public class recursivedescentparser {      // @formatter:off     final static string text = "" +             "[" +              " anchor{anchorname='&loreal_lip_balm'," +              "    clientname='loreal india'," +              "" +              "  anchorprice=10," +              "        campaigns=[" +              "                campaign{" +              "                        campaignid='loreal_camp1'," +              "" +              "                        question='how turn melted lipstick tinted lip balm'," +              "" +              "                        startdate=2015-08-04," +              "                        enddate=2015-08-04," +              "                        imageurl='null'," +              "" +              "                        regioncountry='all'," +              "                        rewardinfo='null'," +              "                        campaignprice=20," +              "" +              "                        options=null" +              "              }" +              "      ]}," +              "    anchor{" +              "" +              "            anchorname='&loreal_total_repair_5'," +              "            clientname='loreal india'," +              "            anchorprice=125," +              "" +              "          campaigns=[" +              "                    campaign{" +              "                    campaignid='loreal_camp2'," +              "                            question='is good" +              "                         product buy?'," +              "                            startdate=2015-08-04," +              "                            enddate=2015-08-04," +              "" +              "                         imageurl='null'," +              "                            regioncountry='all'," +              "                            rewardinfo='null'," +              "" +              "                         campaignprice=20," +              "                            options=null" +              "                    }" +              "                ]" +              "       }" +              " ]" +              "";     // @formatter:on      static class token {         enum tokentype {             open_bracket, close_bracket, open_brace, close_brace, string, name, comma, equals, integer, date, eof, null;         }          private string text;         private tokentype type;          public token(tokentype type) {             this(type, null);         }          public token(tokentype type, string text) {             this.type = type;             this.text = text;         }          public tokentype gettype() {             return type;         }          public string gettext() {             return text;         }          @override public string tostring() {             return "token [text=" + text + ", type=" + type + "]";         }      }      static class tokenreader {         private pushbackreader reader;          public tokenreader(reader reader) {             this.reader = new pushbackreader(reader);         }          public token nexttoken() throws ioexception {             token t = nexttokenx();             system.out.println("got: " + t);             return t;         }          public token nexttokenx() throws ioexception {             int c;             while ((c = reader.read()) != -1 && character.iswhitespace((char) c))                 ;             if (c == -1)                 return new token(tokentype.eof);             switch (c) {             case '[':                 return new token(tokentype.open_bracket);             case ']':                 return new token(tokentype.close_bracket);             case '{':                 return new token(tokentype.open_brace);             case '}':                 return new token(tokentype.close_brace);             case ',':                 return new token(tokentype.comma);             case '=':                 return new token(tokentype.equals);             default:                 if (character.isdigit(c))                     return readintegerordate(c);                 if (c == '\'')                     return readstring(c);                 if (character.isjavaidentifierstart(c))                     return readname(c);                 throw new runtimeexception("invalid character '" + ((char) c) + "' in input");             }         }          private token readname(int c) throws ioexception {             stringbuilder sb = new stringbuilder();             sb.append((char) c);             while ((c = reader.read()) != -1 && character.isjavaidentifierpart(c))                 sb.append((char) c);             if (c != -1)                 reader.unread(c);             if ("null".equals(sb.tostring()))                 return new token(tokentype.null);             return new token(tokentype.name, sb.tostring());         }          private token readstring(int end) throws ioexception {             stringbuilder sb = new stringbuilder();             int c;             while ((c = reader.read()) != -1 && c != end)                 sb.append((char) c);             return new token(tokentype.string, sb.tostring());         }          private token readintegerordate(int c) throws ioexception {             stringbuilder sb = new stringbuilder();             sb.append((char) c);             while ((c = reader.read()) != -1 && character.isdigit((char) c))                 sb.append((char) c);             if (c == '-') {                 sb.append((char) c);                 return readdate(sb);             }             if (c != -1)                 reader.unread(c);             return new token(tokentype.integer, sb.tostring());         }          private token readdate(stringbuilder sb) throws ioexception {             int c;             while ((c = reader.read()) != -1 && character.isdigit((char) c))                 sb.append((char) c);             if (c == -1)                 throw new runtimeexception("eof while reading date");             if (c != '-')                 throw new runtimeexception("invalid character '" + (char) c + "' while reading date");             sb.append((char) c);             while ((c = reader.read()) != -1 && character.isdigit((char) c))                 sb.append((char) c);             if (c != -1)                 reader.unread(c);             return new token(tokentype.date, sb.tostring());         }     }      static class lexer {          private tokenreader reader;         private token current;          public lexer(reader reader) {             this.reader = new tokenreader(reader);         }          public token expect(tokentype... tt) throws ioexception {             if (current == null)                 current = reader.nexttoken();             (tokentype tokentype : tt) {                 if (current.gettype() == tokentype) {                     token r = current;                     current = null;                     return r;                 }             }             throw new runtimeexception("expecting 1 of " + arrays.aslist(tt) + " got " + current);         }          public token expect1or0(tokentype... tt) throws ioexception {             if (current == null)                 current = reader.nexttoken();             (tokentype tokentype : tt) {                 if (current.gettype() == tokentype) {                     token r = current;                     current = null;                     return r;                 }             }             return null;         }     }      public object parse(string text) throws ioexception {         return parse(new stringreader(text));     }      private object parse(reader reader) throws ioexception {         lexer lexer = new lexer(reader);         return parse(lexer);     }      private object parse(lexer lexer) throws ioexception {         token t = lexer.expect1or0(tokentype.open_brace, tokentype.open_bracket, tokentype.eof);         if (t == null || t.gettype() == tokentype.eof)             return null;         else if (t.gettype() == tokentype.open_bracket) {             return parselist(lexer);         } else {             return parsemap(null, lexer);         }     }      private list<object> parselist(lexer lexer) throws ioexception {         arraylist<object> result = new arraylist<object>();         token tname = lexer.expect1or0(tokentype.name);         while (tname != null) {             lexer.expect(tokentype.open_brace);             result.add(parsemap(tname.gettext(), lexer));             if (lexer.expect1or0(tokentype.comma) != null)                 tname = lexer.expect(tokentype.name);             else                 tname = null;         }         lexer.expect(tokentype.close_bracket);         return result;     }      private object parsemap(string oname, lexer lexer) throws ioexception {         map<string, object> result = new hashmap<string, object>();         if (oname != null)             result.put("objectname", oname);         token tname = lexer.expect1or0(tokentype.name);         while (tname != null) {             string name = tname.gettext();             lexer.expect(tokentype.equals);             token next = lexer.expect(tokentype.string, tokentype.date, tokentype.integer, tokentype.open_bracket,                     tokentype.open_brace, tokentype.null);             tokentype tt = next.gettype();             if (tt == tokentype.string) {                 result.put(name, next.gettext());             } else if (tt == tokentype.date) {                 simpledateformat sdf = new simpledateformat("yyyy-mm-dd");                 try {                     result.put(name, sdf.parse(next.gettext()));                 } catch (parseexception e) {                     return new runtimeexception(e);                 }             } else if (tt == tokentype.integer) {                 result.put(name, integer.valueof(next.gettext()));             } else if (tt == tokentype.open_bracket) {                 result.put(name, parselist(lexer));             } else if (tt == tokentype.open_brace) {                 result.put(name, parsemap(null, lexer));             } else {                 result.put(name, null);             }             if (lexer.expect1or0(tokentype.comma) != null)                 tname = lexer.expect(tokentype.name);             else                 tname = null;         }         lexer.expect(tokentype.close_brace);         return result;     }      public static void main(string[] args) throws ioexception {         recursivedescentparser parser = new recursivedescentparser();         object o = parser.parse(text);         system.out.println(o);     }  } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -