How to create JSON in iOS

Discussion in 'iOS Programming' started by Narendar Singh, Jul 24, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    I have few items which are: mobile_name, mobile_model, mobile_size (all are type of NSString)

    I have to send this data on server in following JSON format:
    {
    "Mobile":
    [
    {
    "name": "iPod Touch",
    "model": "Any Model",
    "size": "10",
    }
    ]
    }

    How to create this data in JSON Format?

    My approach is below.

    Code:
    NSMutableString *myJSON= [NSMutableString stringWithString:@"{ \"Mobile\" : [ "]; 
            
    [myJSON appendString: [NSString stringWithFormat: @"{ \"name\" : \"%@\", \"model" : \"%@\", \"size\" : \"%@\" }",                                        
                                                                                  mobile_name, mobile_model, mobile_size]];
            
    [myJSON appendString@" ]}"]; 
    My question is that, Is this approach correct OR are there some APIs or something like that which are used to create JSON in desired format?
     
  2. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #3
    Thanks

    But, Available in iOS 5.0 and later.

    For below than 5 ?
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    What have you tried?

    Did you try googling? What search terms did you use? Be specific.


    http://json.org/
     
  4. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #5
    Actually I haven't tried the api you suggested, I just see it is from 5.0 and later, so I thought I should not go with this because it wont work on below than 5.0

    My another approach is

    Code:
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setValue:@"Value 1" forKey:@"v1"];
    [dict setValue:@"Value 2" forKey:@"v2"];
    
    NSDictionary *finalData = [NSDictionary dictionaryWithObject:dict forKey:@"MyArray"];
            
    NSString *jsonString = [finalData JSONRepresentation];
    
    DebugLog(@"JSON String %@", jsonString);
    
    Is this correct to create JSON from some values.
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    A Google search came up with a few.

    Why are you spending your time with iOS versions at less than 5.0? Are you supporting something old?
     
  6. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #7
    Yes my app should be run from iOS 3.0 and above. This is my requirement.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    That doesn't answer the question I asked. Did you try searching with google for JSON libraries, classes, or other function in Objective-C? If so, what did you find? What were the google search terms you used?

    As already pointed out by xStep, it's very easy to find JSON support for iOS earlier than 5. If we have to tell to do a search first, then you haven't tried anything.

    Conversely, if you haven't told us what you found by searching (searching is something you tried), then you're wasting everyone's time by making us repeat things you've already done. That's why you should answer the question "What have you tried?" and include what you tried to find.

    http://www.mikeash.com/getting_answers.html
    http://WhatHaveYouTried.com/


    Did you try it? What was the result?

    Where did you get the code from? Does it require a supporting library, perhaps one that adds a -JSONRepresentation method to NSDictionary? What is the name of the library? (I can answer those questions by doing a couple of google searches. But me answering them serves no purpose. It's your code, so you need to be able to answer them, because if it doesn't do what you expect, you need to understand why, so you can fix it.)

    Instead of randomly posting things you find without trying them, you should try them and see what happens. If you then have a problem that can't be solved, post a question with details:
    1. What library did you try? What URL did you get it from?
    2. What is the complete code?
    3. What is the expected output?
    4. What is the actual output?
     
  8. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #9
    make sense...

    Well the code I have posted I have tried. Let me summaries what I want or expecting or asking.

    1) My first post in which I am creating JSON - Should I create in this way? IF NOT then goto point no 2

    2) I am using NSDictionary to create JSON. Again is this right way to create JSON? IF NOT then goto to point no 3

    3) There are lots of Libs (already mentioned) and me too found on Google. If above two approaches are not correct then I must go with Libs.

    Issue with Point No 1 - If string has " quotes then it is not converting it to \"
    With Point No 2 - There is no issue, but I was not able to create JSON Array (I'll try again)

    And Point no 3 would be my last option.
     
  9. chown33, Jul 24, 2012
    Last edited: Jul 24, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    What was the output? Is it correct JSON? You did look at the output, didn't you? I can't tell, because you haven't posted any output (point 4 in my list).

    I ask this because the answers you provide are precisely the answers to your questions:
    Does the code in your first post produce correct JSON output? You answered this yourself:
    If string has " quotes then it is not converting it to \"​
    So the answer is clearly "No, this won't work as posted." Your options would be to change it so it works, or try something else.


    Does the code in your NSDictionary post produce correct JSON output? I don't know, because you didn't say you tried it, nor did you post the output, you just posted code and asked if it was correct.

    If you tried compiling it without the necessary library added, then it won't even compile, so it would be incorrect without the additional library. But you haven't said if you're using the necessary library, or whether it compiles or not, so I don't know whether what you posted is correct or not, without knowing whether you added the library.

    I can assure you that it won't compile without the necessary library added. If you got the code from somewhere on the internet, it should have told you what that library is.

    You wrote "There is no issue, ...", which I take to mean it works. Which implies that it compiles and produces correct JSON output. Otherwise you can't really say "There is no issue". So that answers question 2, doesn't it?


    I don't know what ".. but I was not able to create JSON Array" means, because you haven't posted code for an array. If you haven't tried making an array, you should try that, post the code, and post the output.
     
  10. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #11
    I realized that my way of explanation was not good.

    I had to detailed out everything clearly which I couldn't. My mistake.

    Well thanks for all your guidelines and suggestions.

    Let me do some more stuffs regarding this particular post
    and I will try to make my other threads clearer and descriptive so that it wont waste the time of other people.

    :)
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    The two most important things you consistently left out:
    1. You never said "I tried this", for anything you posted.
    2. You never posted any output, nor did you ever say that anything you tried (which you never said) produced correct output or not.

    It's as if you expected us to read your mind, that by posting code it automatically means you tried it, and we could somehow read the output on your screen.
     
  12. Kashsystems macrumors 6502

    Joined:
    Jul 23, 2012
    #13
    Maybe my memory is off here, but anything below ios 5.0 requires a 3rd party parser/framework for JSON. You would have to look into their api documentation to see how to implement the methods to parse your data. Its been over a year but I believe I used touchjson for that kind of work. Back then the only parsing iOS supported was xml.
     

Share This Page