2013-11-28 3 views
0

base64로 저장된 문자열을로드하고 싶지만 항상이 오류가 발생합니다. SimpleJson 클래스 (http://wiki.unity3d.com/index.php/SimpleJSON)를 사용하고 있습니다.JSON (문자열/base64)을 비 직렬화하는 중 오류가 발생 했습니까?

예외 : JSON을 비 직렬화하는 동안 오류가 발생했습니다. 미지의 태그 66 SimpleJSON.JSONNode.Deserialize (System.IO.BinaryReader aReader) (자산/플러그인/SimpleJSON.cs에서 512)

내 코드 : 여기

var I = new JSONClass(); 
I["author"]["name"] = "testName"; 
I["author2"]["name2"] = "testName2"; 
string str = I.SaveToCompressedBase64(); 
//output : QlpoOTFBWSZTWdFZTaIAAAdNgH/gEAAA etc. 

//#Error deserializing JSON 
string res = JSONClass.LoadFromBase64(str);//.ToString(); 

이 방법은 클래스에서 :

public static JSONNode LoadFromBase64(string aBase64) 
     { 
      var tmp = System.Convert.FromBase64String(aBase64); 
      var stream = new System.IO.MemoryStream(tmp); 
      stream.Position = 0; 
      return LoadFromStream(stream); 
     } 

public static JSONNode LoadFromStream(System.IO.Stream aData) 
     { 
      using(var R = new System.IO.BinaryReader(aData)) 
      { 
       return Deserialize(R); 
      } 
     } 


public static JSONNode Deserialize(System.IO.BinaryReader aReader) 
     { 
      JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte(); 
      switch(type) 
      { 
      case JSONBinaryTag.Array: 
      { 
       int count = aReader.ReadInt32(); 
       JSONArray tmp = new JSONArray(); 
       for(int i = 0; i < count; i++) 
        tmp.Add(Deserialize(aReader)); 
       return tmp; 
      } 
      case JSONBinaryTag.Class: 
      { 
       int count = aReader.ReadInt32();     
       JSONClass tmp = new JSONClass(); 
       for(int i = 0; i < count; i++) 
       { 
        string key = aReader.ReadString(); 
        var val = Deserialize(aReader); 
        tmp.Add(key, val); 
       } 
       return tmp; 
      } 
      case JSONBinaryTag.Value: 
      { 
       return new JSONData(aReader.ReadString()); 
      } 
      case JSONBinaryTag.IntValue: 
      { 
       return new JSONData(aReader.ReadInt32()); 
      } 
      case JSONBinaryTag.DoubleValue: 
      { 
       return new JSONData(aReader.ReadDouble()); 
      } 
      case JSONBinaryTag.BoolValue: 
      { 
       return new JSONData(aReader.ReadBoolean()); 
      } 
      case JSONBinaryTag.FloatValue: 
      { 
       return new JSONData(aReader.ReadSingle()); 
      } 

      default: 
      { 
       throw new Exception("Error deserializing JSON. Unknown tag: " + type); 
      } 
      } 
     } 

감사

+0

base64로 압축을 해제하십시오 문자열을 구문 분석하기 전에 문자열. – rdodev

+0

@rdodev 감사합니다. 어떻게 할 수 있습니까? 나는'var base64 = System.Convert.FromBase64String (str); '을 시도하지만 그것을하는 방법이 아닌 것 같다. – Paul

+0

아래 내 대답은 도움이 될 것입니다. – rdodev

답변

1

당신이 겪고있는 문제는 당신이에 저장하려고한다는 것입니다 여기 compressedbase64string : 당신이 그것을 분석하고 그것을 압축을 해제 할 때 당신에게 문제를주고있다

string str = I.SaveToCompressedBase64();

합니다. 그래서, "

string str = I.SaveToBase64();

을 다음과 그리고 (나는 보지 못했다가 또 다른 오류가 아니라면) 변경 프로그램의 나머지 부분을 떠나 당신이 자신의 SaveToBase64()를 사용하는 것이 좋습니다.

을 또 다른 방법을 사용하는 것입니다 . 자신의 LoadFromCompressedBase64(), 그래서 당신의 코드를 제외하고 동일하게 보일 것이다 : 당신은 압축 된 문자열을 구문 분석하려고

string res = JSONClass.LoadFromCompressedBase64(str);//.ToString();

+0

완벽한! 나는 가까웠다 :) 나는 그 방법에서 "압축 된"것에주의를 기울이지 않았다. 아주 좋았어, 고마워! – Paul

관련 문제