XML 到 JSON 转换问题。我想在字典结构数组中更改它。可能吗?

XML to JSON convert issue. I want to change it in array of dictionary structure. is it possible?

我想在字典结构数组中更改它。可能吗?实际上我有一个 XML 结构,在将 xml 转换为 json 时,如果它有一个值,它给出字典结构,否则为数组结构,请问如何 handle/parse它。

一个值的大小写

            item =         {
                "ML_KEY" =             {
                    text = "test1";
                };
                name =             {
                    text = "test2";
                };
                order =             {
                    text = 1;
                };
                value =             {
                    text = "Test3"
                };
            };

超过一个值的情况

 item =             (
                            {
                "ML_KEY" =                     {
                    text = "Testing4";
                };
                name =                     {
                    text = "Testing3";
                };
                order =                     {
                    text = 1;
                };
                value =                     {
                    text = "TestingB";
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "New2";
                };
                name =                     {
                    text = "New";
                };
                order =                     {
                    text = 2;
                };
                value =                     {
                    text = "Preview"
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "Testing2"
                };
                name =                     {
                    text = Remove;
                };
                order =                     {
                    text = 3;
                };
                value =                     {
                    text = Remove;
                };
            },
                            {
                "ML_KEY" =                     {
                    text = "Testing";
                };
                name =                     {
                    text = "E";
                };
                order =                     {
                    text = 4;
                };
                value =                     {
                    text = "D";
                };
            }
        );
    };
    NSDictionary *responseDict = XMLResponse;

    for (NSDictionay * dict in responseDict){
        __do stuff here__
    }

您还可以使用 NSMutableDictionary,这样您就可以在抛出响应时添加多行

您应该检查所获取数据的 class 类型。你可以这样做。

id items =[[NSDictionary dictionaryWithXMLString:string] valueForKey:@"items"];
if ([items isKindOfClass:[NSDictionary class]])
{
  // parse your dictionary here

}else if([items isKindOfClass:[NSArray class]])
{
  // parse your array here

}else{

}

是的,我做的和上面的一样,只是稍作改动:

    id checkDict = [tempDict valueForKeyPath:@"items.item"];
    if ([checkDict isKindOfClass:[NSDictionary class]])
    {
      // parse your dictionary here

    }else if([checkDict isKindOfClass:[NSArray class]])
    {
      // parse your array here

    }else{
}