开发者

Nested NSDictionary with valueForKeyPath returns brackets instead of NSString

Source is JSON in CouchDB (relevant data):

{
"_id":"f994892f3fb525d73b3b6b8a59000e1d",
"_rev":"3-c431ee9334e9be038d9c935efcf2f049",
"teiXML":[
  {
     "teiHeader":[
        {
           "fileDesc":[
              {
                 "publicationStmt":[
                    {
                       "publisher":"University",
                       "pubPlace":"Someplace",
                       "idno type=\"callNo\"":"ABC 007",
                       "date":"2007"
                    }
                 ],

This is the NSDictionary (simplified):

<CCouchDBDocument: 0x5842c0> (id:f994892f3fb525d73b3b6b8a59000e1d rev:4-3fc1a36de622529cd67416c9e5ae88da {
"_id" = f994892f3fb525d73b3b6b8a59000e1d;
"_rev" = "4-3fc1a36de622529cd67416c9e5ae88da";
teiXML =     (
            {
        teiHeader =             (
                            {
                fileDesc =                     (
                                            {
                        publicationStmt =                             (
                                                            {
                                date = 2007;
                                "idno type=\"callNo\"" = "ABC 007";
                                publisher = "University";

I'd like to get the value of "publisher", so I tried using valueForKeyPath:

NSLog(@"%@",[doc valueForKeyPath:@"content.teiXML.teiHeader.fileDesc.publicationStmt.publisher"]);

But instead of the value "University" I get this as output:

1> 2011-05-26 10:10:02.717 MyApp[12770:707] (
        (
                (
                        (
                "University"
            )
        )
    )
)

Which is qu开发者_StackOverflow社区ite annoying. I can get the value with an indexAtObject-combination:

NSLog(@"%@",[[[[[doc valueForKeyPath:@"content.teiXML.teiHeader.fileDesc.publicationStmt.publisher"] objectAtIndex:0] objectAtIndex:0] objectAtIndex:0] objectAtIndex:0]);

Something must be wrong there? There are more values to extract and I don't want to count how many objectAtIndex-repetitions I need to make it work.


You are mentioning the "xml" tag, so I suppose that your NSDictionary is being built from some XML data. In this case, one hypothesis that could explain this behavior is that your XML contains repeated keys, like in:

<values>
  <value>
  ......
  </value>
  <value>
  ......
  </value>
  ...
</values>

Indeed, repeated values like in the examples are not suitable to be treated through Key-Value Coding and are handled internally as NSArrays. Now, the actual details of why you get so many nested arrays in your reply are difficult for me to figure out, but this could give you a hint at what is happening.

If this hypothesis does not apply to your case, please share the way you build your NSDictionary.

EDIT: after looking at your JSON, there is something that looks not obvious to me. Look at this:

"publicationStmt":[
                {
                   "publisher":"University",
                   "pubPlace":"Someplace",
                   "idno type=\"callNo\"":"ABC 007",
                   "date":"2007"
                }
             ],

what I understand is you have nested arrays: plublicationStmt contains an array ([) which at turns contain another (keyed) array ({)...

Is this possibly that produce your result?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜