1

以下のコードを使用して XMLString を JSONstring に変換しています。また、改行と空白を置き換えてこの XML を 1 行に変換しようとしましたが、出力に変化はありません。

NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&parseError];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&error];
if (! jsonData)
{
    NSLog(@"Got an error: %@", error);
}
else
{
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

ここに私のサンプルXMLStringがあります

<component>
        <section ID="id_0b9b2dbb-ac86-4aad-b567-00a78d9e18a7">
           <id root="7d973a3a-d4fa-440c-b9fd-7bf0f9bfc1c4"/>
           <code code="34066-1" codeSystem="2.16.840.1.113883.6.1" displayName="Boxed Warning section"/>
           <title>WARNING</title>
           <text>
              <paragraph>Suicidality and Antidepressant Drugs</paragraph>
              <paragraph>
                 <content styleCode="bold">
                    <content styleCode="italics">Use in Treating Psychiatric Disorders:        </content>
                 </content> Antidepressants increased the risk compared to placebo of suicidal thinking and behavior (suicidality) in children, adolescents, and young adults in short-term studies of major depressive disorder (MDD) and other psychiatric disorders. Anyone considering the use of WELLBUTRIN XL<sup>®</sup>* or any other antidepressant in a child, adolescent, or young adult must balance this risk with the clinical need. Short-term studies did not show an increase in the risk of suicidality with antidepressants compared to placebo in adults beyond age 24; there was a reduction in risk with antidepressants compared to placebo in adults aged 65 and older. Depression and certain other psychiatric disorders are themselves associated with increases in the risk of suicide. Patients of all ages who are started on antidepressant therapy should be monitored appropriately and observed closely for clinical worsening, suicidality, or unusual changes in behavior. Families and caregivers should be advised of the need for close observation and communication with the prescriber. WELLBUTRIN XL is not approved for use in pediatric patients. (See <content styleCode="bold"<linkHtml href="#i4i_warnings_id_2dfa5d10-0696-41d8-bc4b-d82cd933a761">WARNINGS: Clinical Worsening and Suicide Risk in Treating Psychiatric Disorders</linkHtml>, <linkHtml href="#i4i_precautions_id_8611ce24-cbea-4cf8-81fe-3efe8c368279">PRECAUTIONS: Information for Patients</linkHtml>, and <linkHtml href="#i4i_precautions_id_8611ce24-cbea-4cf8-81fe-3efe8c368279">PRECAUTIONS: Pediatric Use</linkHtml>.)</content>
</paragraph>
</text>
<effectiveTime value="20100602"/>
</section>
</component>

これが私のJSON出力です:

"component" : [
            {
              "section" : {
                "ID" : "Treating",
                "id" : {
                  "text" : "\n               \n                  \n                   ",
                  "root" : "2724b846-267b-4dbb-bd9b-ec4db83617ff"
                },
                "code" : {
                  "code" : "42229-5",
                  "displayName" : "SPL UNCLASSIFIED SECTION",
                  "text" : "\n                     ",
                  "codeSystem" : "2.16.840.1.113883.6.1"
                },
"title" : {
                  "text" : "\n                     Suicidality and Antidepressant Drugs"
                },
                "text" : "\n                  ",
                "effectiveTime" : {
                  "value" : "20120823",
                  "text" : "\n                     "
                }
              },
              "text" : "\n               "
            },

そして、この JSON を取得しました。正確なデータで適切な JSON を取得するのを手伝ってください。あなたが見ることができるテキストを取得していません:

"text" : "\n               \n                  \n                   ",
4

2 に答える 2

1

NSJSONWritingPrettyPrintedの代わりに KnilOption を試してください

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&error];
于 2013-10-18T15:10:12.187 に答える