0

I'm having an issue getting my UIImage to work from the JSon that I received. Here let's take a look at my JSON

[
{
node_title: "Fumi",
node_uid: "11",
users_node_name: "pae",
nid: "7",
Body: "<p>This is Fumi Restaurant</p> ",
Enterprise Address: "99/9",
Enterprise Email: "[email protected]",
Enterprise Tel: "08111111",
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fumi.jpg" width="300" height="300" alt="" />"
},
{
node_title: "Fuji",
node_uid: "8",
users_node_name: "testent",
nid: "1",
Body: "<p>Fuji</p> ",
Enterprise Address: "Somwhere it belong",
Enterprise Email: "[email protected]",
Enterprise Tel: "02-999-9999",
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />"
}
]

and Here is my code: I'm able to get all the Json that I received and show them on this ViewController. However, for the [self.detail valueForKey:@"Enterprise Logo"] doesn't seems to work due to the content of the JSON.

BTW, self.detail is the Json object that I passed from the previous UITableViewController by doing preparedtosegue.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.EnterPriseName.text = [self.detail valueForKey:@"node_title"];
    self.EnterPriseTel.text = [self.detail valueForKey:@"users_node_name"];
    self.EnterPriseBody.text = [self.detail valueForKey:@"Body"];
    self.EnterPriseEmail.text = [self.detail valueForKey:@"nid"];

    [self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]];

}

I also try this method which works just fine but the content in Enterprise Logo must contain only the URL of that img only. I'm trying to do things as dynamic as much as possible, so I don't expect the static answer. Is there a way for me to fix this issue ?

[self.picLogo setImageWithURL:[NSURL URLWithString:@"http://localhost/drupal/sites/default/files/Fumi.jpg"]];
6
  • 2
    That's not legal JSON. It will not parse.
    – Hot Licks
    Apr 6, 2014 at 14:24
  • @HotLicks how do I make it legal then ?
    – Investor06
    Apr 6, 2014 at 14:38
  • 1
    Have the other end generate legal JSON.
    – Hot Licks
    Apr 6, 2014 at 14:40
  • The above "JSON" sort of looks line an NSLog of a NSArray/NSDictionary tree generated from JSON, except that NSLog logs an array bracketed with (), and uses = instead of :. I have no idea how you generated the above, and no clue how to interpret it. If it were valid JSON the key values would be in quotes and the " characters in the "Enterprise Logo" string would be properly escaped.
    – Hot Licks
    Apr 6, 2014 at 14:44
  • @HotLicks I got it working now thank Hot Licks I still couldn't implement the question from yesterday thought T_T stackoverflow.com/questions/22870595/…
    – Investor06
    Apr 6, 2014 at 16:13

3 Answers 3

2
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />"

The vale for the Enterprise Logo key is html in your json.

[self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]];

The code you are using is looking for a URL to that image. not the html.

Best practice would be to just have the json have the image url in the format.

http://localhost/drupal/sites/default/files/Fuji.png

and not the html in it.

1
  • Explain how that even parses.
    – Hot Licks
    Apr 6, 2014 at 14:32
0

After retrieving the value of Enterprise Logo just extract the image URL and request the image using NSURLConnection, NSURLSession, or AFNetworking.

e.g.

NSString * __block imageURL = nil;
NSArray * objects = [[self.details valueForKey:@"Enterprise Logo"] componentsSeparatedByString:@" "];

[objects enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop)
{
    NSRange range = [object rangeOfString:@"src="];
    if (range.location != NSNotFound)
    {
        imageURL = [object substringFromIndex:range.location+range.length];
        *stop = YES;
    }
}];
3
  • I followed your method but it doesn't work I NSLog the imageURL out and it is still null.
    – Investor06
    Apr 6, 2014 at 14:27
  • 1
    As @HotLicks mentioned in his comment, the JSON is not legal so it will not parse which is why you are getting a null value.
    – Jonathan
    Apr 6, 2014 at 14:37
  • 1
    I got them working now after following @HotLicks comment Thank you very for your help guys! i'm really appreciated
    – Investor06
    Apr 6, 2014 at 16:12
0

Extract src value from img tag of Enterprise Logo that will be the url for image.

To extract the Url, please look into this link: retrive image url from string

Let me know if you have any issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.