2012-02-08 6 views
0

私の仕事は、XML WADLでXMLからJSONへの変換ツールを実行することによって作成されたJSONを解析することです。レスポンスで返されたデータをJSONに解析でき、データから最上位のNSDictionaryを取得できます。私は辞書から配列を取得することができますし、配列からNSDictionary(それはキー値が含まれていますがそれ)と私は行くことができるほど深いthats。私は過去に構文解析を行ってきましたが、この特定のJSON形式は私に運がないのですか?ここで私はここ..Object-CでのJSONの構文解析の失敗

// Decode the data 
NSError *parseError = nil; 
NSData *jsonData = [request responseData]; 
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&parseError]; 
if (parseError) { 
    NSLog(@"Error: %@", [parseError localizedDescription]); 

    // other stuff.. 
    return; 
} 

NSLog(@"JSON=: %@", responseDict.description); 
NSArray *messageBoxResponse = [responseDict objectForKey:@"messageBoxResponse"]; 
NSLog(@"messageBoxResponse=: %@", messageBoxResponse); 
NSUInteger count = messageBoxResponse.count; 
for (NSDictionary* messages in messageBoxResponse) { 

    NSLog(@"Messages=: %@", messages.description); 
    NSArray *message = [messages objectForKey:@"message"]; 

    // get SIGABRT 
    count = message.count; 

    // If I comment out above line and introduce this I get SIGABRT also 
    for (NSDictionary* something in message) { 
     NSLog(@"Somthing=: %@", something.description); 


} 

を使用しているコードは、私が解析しようよJSONは、IPアドレスが:)に変更されました。..です:

{ 
messageBoxResponse =  { 
    messages =   { 
     link =    { 
      href = "http://1.1.1.1:80/services/messageboxes/63358/messages"; 
      rel = self; 
     }; 
     message =    (
          { 
       flags =      { 
        answered = 0; 
        link =       { 
         href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags"; 
         rel = self; 
        }; 
        taggedForDeletion = 0; 
        unread = 0; 
       }; 
       from = "[email protected]"; 
       link =      { 
        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>"; 
        rel = self; 
       }; 
       parts =      { 
        link =       { 
         href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts"; 
         rel = self; 
        }; 
        part =       { 
         link =        { 
          href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0"; 
          rel = self; 
         }; 
         mimeType = "TEXT/PLAIN"; 
         name = "text.txt"; 
         size = 564; 
        }; 
       }; 
       receivedDate = 1328624061000; 
       sentDate = 1328624035000; 
       subject = test; 
      }, 
          { 
       flags =      { 
        answered = 0; 
        link =       { 
         href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/flags"; 
         rel = self; 
        }; 
        taggedForDeletion = 0; 
        unread = 0; 
       }; 
       from = "[email protected]"; 
       link =      { 
        href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>"; 
        rel = self; 
       }; 
       parts =      { 
        link =       { 
         href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts"; 
         rel = self; 
        }; 
        part =       (
                { 
          link =         { 
           href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/0"; 
           rel = self; 
          }; 
          mimeType = "TEXT/PLAIN"; 
          name = "text.txt"; 
          size = 6; 
         }, 
                { 
          content =         { 
           href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1/2c3a3400620f218d5378607260dc2749.wav"; 
           rel = content; 
          }; 
          duration = 3; 
          link =         { 
           href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/1"; 
           rel = self; 
          }; 
          mimeType = "AUDIO/WAV"; 
          name = "Audio_Recording_S000551_002.wav"; 
          size = 31190; 
         }, 
                { 
          link =         { 
           href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<[email protected]>/parts/2"; 
           rel = self; 
          }; 
          mimeType = "APPLICATION/MS-TNEF"; 
          name = "winmail.dat"; 
          size = 656; 
         } 
        ); 
       }; 
       receivedDate = 1328562604000; 
       sentDate = 1328562596000; 
       subject = "Voice Message from Suren 1 (63357)"; 
      } 
     ); 
    }; 
}; 

}

+2

有効なJSONを最初に解析しようとしていることを検証する必要があります。 http://jsonlint.com/または他のバリデーターをチェックして、入力を最初に確認してください。次に、iOS 5を使用している場合は、 'NSJSONSerialization'を使用できます。それ以外の場合は、https://github.com/johnezang/JSONKitのような別のライブラリを使用して大変な作業をすることができます。 –

答えて

0

そのため、 jsonは無効です。

{ 
    "messages": { 
     "link": { 
      "href": "http: //1.1.1.1: 80/services/messageboxes/63358/messages", 
      "rel": "self" 
     } 
    } 
} 

'='は ':'にする必要があります。そこには存在しないセミコロンがあります。

+0

ありがとうございました。正しい形式ではありません。私は、同じリクエスト "SOAPUI"と "WizTools"を発行するために、いくつかの安らかなクライアントを使用してみました。どちらの場合も有効なXMLを返します。しかし、プログラムでは、NSJSONSerializationを使用すると返される形式が異なり、有効ではありません。私はおそらくエンコーディングの違いを考えていますが、この時点で私は知らないのですか? – imobilizer

+0

実際、JSONは要求から取得するときに有効な文字列です。その文字列をNSDictionaryに変換すると、引用符とコロンが置き換えられ、JSONが無効になります。次の両方のメソッドは無効なJSONをレンダリングします – imobilizer

+0

実際、JSONは要求から取得するときに有効な文字列です。その文字列をNSDictionaryに変換すると、引用符とコロンが置き換えられ、JSONが無効になります。 NSJSONSerialization:dataWithJSONObjectとSBJSONParser objectwithDataの両方が無効なJSONを持つNSDictionaryオブジェクトを作成しました。 SBJSON NSDictionaryカテゴリメソッドJSONRepresentationを呼び出す必要がありますが、辞書は解析できません。 – imobilizer