2011-12-02 6 views
1

私はその前のフォーム、ArrayListに文字列をデシリアライズするために時間を探しています。 疑い、私が試した一つ一つが、少なくとも一つのオブジェクトやコマンドがありませんでしたnet.Butには多くの例があります。だから、誰もその道に踏み込むことができず、それを公表した。JSON/Javaの/アンドロイド:文字列からのArrayListへのデシリアライゼーション<Strings>

サーバーは、次の実装があります

for (String string:DBdata) 
    { //put each string in DBdata to a JSON-Object with key=time and value=value 
     jSONString.put(string.substring(0, string.indexOf(",")), string.substring(string.indexOf(",")+1,string.indexOf(";"))); 
    } 

dbdataの中の各文字列は123234のようなものです:1234567。 (UnixTimeOrIndex:値;)デシリアライゼーション後の出力は2dimensionalだろう場合 、細かすぎるだろう。

ありがとうございました。

+1

GSONやJacksonのようなサードパーティのライブラリを使用することを検討してください。開発者の生活を楽にするはずです。 – yorkw

+0

ジャクソンやJSON-シンプルなGoogleからの私の提言のようになります。http://code.google.com/p/json-simple/ – duffymo

答えて

1

私はそれを自分で書いた:おそらく ない最も効果的な解決策が、それは動作します。..メインクラスのすべてがちょうどテストするための文字列を組み立てています。

package test; 
import java.util.ArrayList; 
import net.sf.json.JSONObject; 
public class Tst { 
public static void main(String[] args) { 
     ArrayList<String> versuch=new ArrayList<String>(); 
     for(int i=1; i<11; i++){  
     String temp = "Time1234"+i+",MeanValue123"+i+";"; 
     System.out.println(temp); 
     versuch.add(temp); 
     } 
     System.out.println(versuch); 

     JSONObject jSONString = new JSONObject(); 
     for (String string:versuch) 
     { //put each string in DBdata to a JSON-Object with key=time and value=value 
      jSONString.put(string.substring(0, string.indexOf(",")), string.substring(string.indexOf(",")+1,string.indexOf(";"))); 
     } 
     String output="data.ID=1234."+jSONString.toString(); 
     System.out.println(output); 
     System.out.println(JSONDeconstruct(output)); 
    } 

    public static ArrayList<String> JSONDeconstruct (String st) 
    { 

     ArrayList<String> out=new ArrayList<String>(); 

     int begpos=st.indexOf("{"); 
     int endpos=st.indexOf("}"); 
     int index=0; 

     String work=st.substring(begpos+1, endpos); 
     String replaced=work.replace("\",\"", ","); 
     work=replaced.replace("\":\"", ":"); 
     replaced=work.replace("\"","")+",definedend"; 
     System.out.println(replaced); 

     while (!replaced.equals("definedend")) 
     {   
      out.add(replaced.substring(0,replaced.indexOf(":"))+","+replaced.substring(replaced.indexOf(":")+1, replaced.indexOf(","))+";"); 

      String tempstring=replaced.substring(replaced.indexOf(",")+1); 
      replaced=tempstring; 
      index++; 
      System.out.println("loop disassembly step"+index+" "+replaced); 

     } 


     return out; 


    } 

} 
関連する問題