-1

i have a javascript DTO as given below . what is the structure to convert as a json object using params=DTO list given below should come here in objective-c??

How can i construct the JSON object based on this?

private List<Long> sizes=new ArrayList<Long>();
private List<Long> colors=new ArrayList<Long>();
private List<Long> styles=new ArrayList<Long>();
private List<String> gender=new ArrayList<String>();
private List<Long> brands=new ArrayList<Long>();
private Long vendor;
private String vendorName;
private Boolean isNewArrival=false;
private Boolean isSort=false;
private Boolean isSale=false;
private Boolean isNew=false;
private Boolean isVintage=false;
private Boolean isComingSoon=false;
private Long saleSize;
private Double minPrice=1.0;
private Double maxPrice=5000.0;
private Integer minSalePercentage=0;
private Integer maxSalePercentage=70;
private String socialCategory; 
Preethi
  • 195
  • 1
  • 10
  • Your code example is C#, which is neither Objective-C or Javascript. What did you try? Can you show an example of something you attempted to solve the problem for yourself? – RyanR Oct 28 '13 at 02:35

2 Answers2

3

So DTO is a design pattern nothing more. Read more

You can just create a some class with same properties.

Like:

//.h
@interface SomeClassDTO : NSObject
@property (nonatomic, strong) NSArray *sizes;
@property (nonatomic, strong) NSArray *colors;
...
@property (nonatomic, assign) long vendor;
...
@end

//.m
@implementation SomeClassDTO
@end
tikhop
  • 2,012
  • 14
  • 32
1

Create NSData from NSDictionary: How can I convert NSDictionary to NSData and vice versa? Then you can create a json object,link below: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html

Create a class based on your DTO, (I have used local variables) and fill the dictionary and then use the NSJSONserialization method (with the desired reading options) to create the jSON object. The crude example is:

        NSArray *colorArray = [[NSArray alloc]initWithObjects:@"Blue",@"Gray",nil ];

        NSString *vendorName = @"Name";
        double minPrice = 800.0;
        int minSalePercentage = 20;;

        NSMutableDictionary *jSonDictionary = [[NSMutableDictionary alloc]init];

        //Convert primitive types to NSNumber
        NSNumber *minPriceNum= [NSNumber numberWithDouble:minPrice];
        [jSonDictionary setObject:minPriceNum forKey:@"minPrice"];

        NSNumber *salePercentNum= [NSNumber numberWithInt:minSalePercentage];
        [jSonDictionary setObject:salePercentNum forKey:@"minSalePercentage"];

        [jSonDictionary setObject:vendorName forKey:@"vendorName"];
        [jSonDictionary setObject:colorArray forKey:@"colors"];

        //Convert the dictionary containing DTO values into NSData.
        NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:jSonDictionary]; 

After this you can use : +(id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error

Community
  • 1
  • 1
Rohit Kashyap
  • 934
  • 2
  • 8
  • 23