Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用XMLReader将XML转换为JSON删除文本_Iphone_Ios_Xml_Json - Fatal编程技术网

Iphone 使用XMLReader将XML转换为JSON删除文本

Iphone 使用XMLReader将XML转换为JSON删除文本,iphone,ios,xml,json,Iphone,Ios,Xml,Json,我使用下面的代码将XMLString转换为JSONstring。我还尝试通过替换换行符和空格将此XML转换为单行,但输出中没有任何更改 NSError *parseError = nil; NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:[response dataUsingEncoding:NSUTF8StringEncoding] error:&parseError]; NSError *error; NSD

我使用下面的代码将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;请帮助获取正确的JSON和准确的数据。我没有收到您可以看到的文本:

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

作为替代,您可以使用而不是XMLReader。

作为替代,您可以使用而不是XMLReader。

尝试使用KnilOption而不是NSJSONWritingPrettypted

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&error];

尝试使用针织而不是NSJSONWRITINGPRETTIPTED

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&error];

现在你得到了json…你还想要什么?但是我没有得到没有数据的json,正如你在我的问题中看到的,许多文本返回空白,并带有“\n”,很抱歉我的英语不好。“text”:“\n”您的JSON输出包含XML中不存在的值(如“SPL UNCLASSIFIED SECTION”),因此我严重怀疑这是否是XML到JSON转换的真实输出。-请显示实际输入、实际输出和预期输出。你好,Martin R,我更新了我的代码soryy。现在你得到了json…你还想要什么?但是我没有得到没有数据的json,正如你在我的问题中看到的那样,许多文本返回空白,并带有“\n”,很抱歉我的英语不好。“text”:“\n”您的JSON输出包含XML中不存在的值(如“SPL UNCLASSIFIED SECTION”),因此我严重怀疑这是否是XML到JSON转换的真实输出。-请显示实际输入、实际输出和预期输出。Hello Martin R我为此更新了我的代码soryy。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。在JSON输出中,我们可以看到XML数据中不正确处理文本值的结果。这就是XMLReader的工作方式。XMLConverter处理这些值​​正确地使用XMLConverter与XMLReader没有什么不同,答案中给出了链接。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。在JSON输出中,我们可以看到XML数据中不正确处理文本值的结果。这就是XMLReader的工作方式。XMLConverter处理这些值​​正确地使用XMLConverter与XMLReader没有什么不同,答案中给出了链接。