1

我正在使用下面的代码将 XMLString 转换为 JSONstring。我还尝试通过替换换行符和空格将此 XML 转换为单行,但输出没有任何变化。

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

3

作为替代方案,您可以使用XMLConverter代替 XMLReader。

于 2013-10-18T15:06:30.443 回答
1

尝试使用KnilOption而不是 NSJSONWritingPrettyPrinted

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