java - How to get up-to-date data from dynamically updated json? -
there's chat content want parse. got url .json of it. looks like:
{"messages":[ {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:54","chatid":"111"}, {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:54","chatid":"111"}, {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:54","chatid":"111"} ]}
but json has limitation, think approximately 20-30 records. new records added @ beginning. looks like:
{"messages":[ {"id":"222222","uid":"33333","name":"user","message":"message","date":"2013-02-15 18:21:59","chatid":"111"}, {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:54","chatid":"111"}, {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:55","chatid":"111"} ]} ....... {"messages":[ {"id":"333333","uid":"44444","name":"user","message":"message","date":"2013-02-15 19:13:34","chatid":"111"}, {"id":"222222","uid":"33333","name":"user","message":"message","date":"2013-02-15 18:21:59","chatid":"111"}, {"id":"111111","uid":"22222","name":"user","message":"message","date":"2013-02-15 17:21:54","chatid":"111"} ]}
i gonna read json via gson or json java , place output, doesn't matter :)
but there best-practices on how parse new records in dynamically updated json? in fact don't know how command updated, reading every sec , set results output result in info duplication think.
you need piece info way... not create gson parse slow when file grows, think need preprocess file, slicing disposable data. like:
first execution: parse file exclusively , store first id, since newest data; second execution (and others): read file , store in stringbuilder. using string obtained, piece disposable data, since have id stored prior. id show need start slicing. new data, parse gson , stores first id again.you may utilize create code perform slicing , adapt thought said below:
string info = "{\"messages\":[" + "{\"id\":\"333333\",\"uid\":\"44444\",\"name\":\"user\",\"message\":\"message\",\"date\":\"2013-02-15 19:13:34\",\"chatid\":\"111\"}," + "{\"id\":\"222222\",\"uid\":\"33333\",\"name\":\"user\",\"message\":\"message\",\"date\":\"2013-02-15 18:21:59\",\"chatid\":\"111\"}," + "{\"id\":\"111111\",\"uid\":\"22222\",\"name\":\"user\",\"message\":\"message\",\"date\":\"2013-02-15 17:21:54\",\"chatid\":\"111\"}" + "]}"; string lastid = "111111"; int sliceuntil = data.indexof( "{\"id\":\"" + lastid + "\"" ); // since disposable info in "tail" file, // need valid info (the info until "last id") // , add together chars "]" , "}" close json string newdata = data.substring( 0, sliceuntil ) + "]}"; system.out.println( newdata );
java json
No comments:
Post a Comment