NSArray Returning String Instead of Dictionary

mystic cola

I've setup logs and checked the code and NSArray is being returned as a string instead of a dictionary. The problem is that I'm not sure how I can convert this particular string into a dictionary and I'm running on a tight timeframe.

Here's the code:

NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
        for (NSDictionary *placeJsons in placeJson) {

            NSLog(@"%@",NSStringFromClass([placeJsons class]));
            page = placeJsons[@"page"];        
            NSLog(@"%d",page);
        if (page > 0) {
            NSDictionary* pJson = placeJsons[@"data"];
            for (NSDictionary *dict in pJson) {

Error in Logs and results of NSlog:

2013-09-12 19:50:55.117 GetDeal[1584:c07] __NSCFString
2013-09-12 19:50:55.118 GetDeal[1584:c07] -[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xac5f890
2013-09-12 19:50:55.119 GetDeal[1584:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0xac5f890'

This code may be useful as well.

NSLog(@"AES:%@",aesResponse);
        NSData* jsonResponse = [aesResponse dataUsingEncoding:NSUTF8StringEncoding];



        NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];

Sorry. The JSON data:

http://mobapps.getdeal.me/getcoupons.php?category=art

Possible supe solution:

NSArray* placeJson = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:nil];
        NSDictionary *placeJsons = [NSJSONSerialization JSONObjectWithData:[placeJson dataUsingEncoding:NSUTF8StringEncoding]

Error Message.

No visible @interface for 'NSArray' declares the selector 'dataUsingEncoding:'

Definitely sorry... but in about 30 minutes my life is going to change dramatically.

Answer to Martin:

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
        NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];
        **NSURL *url = [NSURL URLWithString:placeLink];**
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        [request setHTTPMethod:@"POST"];

Config.h

#define placeLink @"http://mobapps.getdeal.me/getcoupons3.php"
Martin R

I will give it a try. As I said in a comment, your provider sends "nested JSON":

NSURL *url = [NSURL URLWithString:@"http://mobapps.getdeal.me/getcoupons.php?category=art"];
NSData *jsonResponse = [NSData dataWithContentsOfURL:url];
NSLog(@"%@", [[NSString alloc] initWithData:jsonResponse encoding:NSUTF8StringEncoding]);

Output:

"{\"data\":[[{\"PID\":\"90205\",\"PName\":null, ... ,\"page\":\"1\"}"

This is JSON data once more encoded as a JSON string. The first task is to remove the outer JSON encoding:

NSError *error;
NSString *outerJson = [NSJSONSerialization
               JSONObjectWithData:jsonResponse
               options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@", outerJson);

Output:

{"data":[[{"PID":"90205","PName":null, ... ,"page":"1"}

So now we have proper JSON data, which can be de-serialized:

NSDictionary *innerJson = [NSJSONSerialization
               JSONObjectWithData:[outerJson dataUsingEncoding:NSUTF8StringEncoding]
               options:0 error:&error];
NSLog(@"%@", innerJson);

Output:

{
    data =     (
                (
                        {
                PAddr = "Save 20-85% on select artwork in the closeout section. Just click the link, then click \"paintings on closeout\" toward the bottom of the page. No code necessary, prices are as marked.";
                PCity = "";

                ...

                rate = 20;
                sao = 20;
            };
        }
    );
    페이지 = 1;
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Returning NSArray element

분류에서Dev

Returning 'Range' instead of actual string in Google Apps Script

분류에서Dev

Google gson.toJson(List) returning response as string instead of array

분류에서Dev

Returning string is not successful

분류에서Dev

Function returning pointer to string

분류에서Dev

c function returning nan instead of double

분류에서Dev

SelectedItem.Text returning selectedvalue instead

분류에서Dev

Weird issue with returning empty struct instead of NULL

분류에서Dev

MySQL returning the MIN id instead of the MAX id?

분류에서Dev

Dictionary <string, Dictionary <T, T []> []> 생성

분류에서Dev

String manipulation with regex and nsarray and mutable strings in objective C

분류에서Dev

Dictionary (Of String, Object)를 Dictionary (Of String, String)로 변환

분류에서Dev

From List<string> to Dictionary<string,string>

분류에서Dev

From string to Dictionary key and value

분류에서Dev

Dictionary <string, class> 생성

분류에서Dev

Django views returning only one particular field instead of entire model

분류에서Dev

MySQL Query returning all dates instead of specified year and month clauses

분류에서Dev

Python CSV library returning 1 item instead of a list of items

분류에서Dev

How to force J to give error instead of incorrectly returning infinity?

분류에서Dev

json_decode is returning a null value instead of an array

분류에서Dev

Find value in dictionary of type <string, string[]>

분류에서Dev

Returning string from BlackBerry java application

분류에서Dev

Ruby String variable returning true for .is_a?(Array)

분류에서Dev

Bad formatted expr returning empty string in Tcl

분류에서Dev

Returning array in Javascript function after splitting string

분류에서Dev

Why is my Swift string variable returning nil?

분류에서Dev

Dictionary <string, Dictionary <string, Dictionary <string, string >>>에서 containsKey를 확인하는 방법

분류에서Dev

`Dictionary <string, Dictionary <string, List <MyCustomClass >>>`를`Dictionary <string, List <MyCustomClass >>`로 변환하는 방법

분류에서Dev

Using LINQ Datarow -> string instead of Datarow -> String[] -> String,

Related 관련 기사

  1. 1

    Returning NSArray element

  2. 2

    Returning 'Range' instead of actual string in Google Apps Script

  3. 3

    Google gson.toJson(List) returning response as string instead of array

  4. 4

    Returning string is not successful

  5. 5

    Function returning pointer to string

  6. 6

    c function returning nan instead of double

  7. 7

    SelectedItem.Text returning selectedvalue instead

  8. 8

    Weird issue with returning empty struct instead of NULL

  9. 9

    MySQL returning the MIN id instead of the MAX id?

  10. 10

    Dictionary <string, Dictionary <T, T []> []> 생성

  11. 11

    String manipulation with regex and nsarray and mutable strings in objective C

  12. 12

    Dictionary (Of String, Object)를 Dictionary (Of String, String)로 변환

  13. 13

    From List<string> to Dictionary<string,string>

  14. 14

    From string to Dictionary key and value

  15. 15

    Dictionary <string, class> 생성

  16. 16

    Django views returning only one particular field instead of entire model

  17. 17

    MySQL Query returning all dates instead of specified year and month clauses

  18. 18

    Python CSV library returning 1 item instead of a list of items

  19. 19

    How to force J to give error instead of incorrectly returning infinity?

  20. 20

    json_decode is returning a null value instead of an array

  21. 21

    Find value in dictionary of type <string, string[]>

  22. 22

    Returning string from BlackBerry java application

  23. 23

    Ruby String variable returning true for .is_a?(Array)

  24. 24

    Bad formatted expr returning empty string in Tcl

  25. 25

    Returning array in Javascript function after splitting string

  26. 26

    Why is my Swift string variable returning nil?

  27. 27

    Dictionary <string, Dictionary <string, Dictionary <string, string >>>에서 containsKey를 확인하는 방법

  28. 28

    `Dictionary <string, Dictionary <string, List <MyCustomClass >>>`를`Dictionary <string, List <MyCustomClass >>`로 변환하는 방법

  29. 29

    Using LINQ Datarow -> string instead of Datarow -> String[] -> String,

뜨겁다태그

보관