0

In my application , i have used the Json.This is my JSON response in my application

This is my Response:
[
    {
    "response": "Success",
    "errorMsg": "",
    "userId": "1",
    "userCompany": "xxxyy",
    "userName": "sham",
    "userAddress": "chennai",
    "userCity": "xxxxx",
    "userMobile": "xxxx",
    "userEmail": "xxx"
},
{
    "response": "Success",
    "errorMsg": "",
    "productImage": "http://www.iii.jpg",
    "productDescription": "Loaded box on pallets - Bart's package",
    "productCost": "10",
    "productBoxWeight": "10.0"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1.4",
    "transportCountry": "Colombia",
    "transportPort": "Havana"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.7",
    "transportCountry": "Brazil",
    "transportPort": "Santos"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "South Africa",
    "transportPort": "Durban"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "Chili",
    "transportPort": "San Antonio"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "2.7",
    "transportCountry": "Australia",
    "transportPort": "Maersk"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Marocco",
    "transportPort": "Casablanca"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Kuwait",
    "transportPort": "Shuwaikh"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Jordan",
    "transportPort": "Aqaba"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.8",
    "transportCountry": "Saoudi Arabia",
    "transportPort": "Jeddah"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.8",
    "transportCountry": "Malta",
    "transportPort": "Maraxklokk"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "0.9",
    "transportCountry": "Mexico",
    "transportPort": "Veracruz"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1.2",
    "transportCountry": "Thailand",
    "transportPort": "Bangkok"
},
{
    "response": "Success",
    "errorMsg": "",
    "transportCost": "1",
    "transportCountry": "Thailand",
    "transportPort": "havana"
}
]

How can i retrive first two set in one array and other sets in another array...i'm new to this please help me to fix it...

6
  • can u pls give me correct info what u need exatctly from that response ?@IOSdev Commented Jun 11, 2013 at 12:05
  • { "response": "Success", "errorMsg": "", "userId": "1", "userCompany": "xxxyy", "userName": "sham", "userAddress": "chennai", "userCity": "xxxxx", "userMobile": "xxxx", "userEmail": "xxx" },i need to separate each set like this... Commented Jun 11, 2013 at 12:07
  • 1
    change your response like key : value manner then read response and save in JsonArray. based on key you can manage response to save appropriate array. Commented Jun 11, 2013 at 12:08
  • Is it possible to change your response ? Commented Jun 11, 2013 at 12:24
  • ur json format not a correct format dear ..pls check in jsonlint or jsonviewer? Commented Jun 11, 2013 at 12:26

2 Answers 2

0

JSON does not define the keys or values (including values in JSON's array) to be in any order. Therefore you'll have to sort the items yourself after parsing.

And in order to parse JSON you can use any JSON parser available (I prefer SBJson). Usually parsers provide possibility to convert JSON to NSDictionary so you can easily deal with its content.

Sign up to request clarification or add additional context in comments.

Comments

0

In order to parse your JSON and get Objective-C objects, use the following piece of code (where data is the JSON you just got):

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data 
                                          options:NSJSONReadingMutableContainers 
                                          error:&e];

After that you'll have a NSArray containing multiple dictionaries (NSDictionary). To get the first one, do:

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
    // get the first dictionary
    NSDictionary *dict = [jsonArray objectAtIndex:0];    
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.