-1

Hello guys i am facing problem related to convert string to json. Here is my String :

[
    {
        "SCHEME_NAME": "FUG RSA SCHEME",
        "Investment_Value": -46719.00201558,
        "Bid_Price": 2.2566,
        "Total_Contributions": 0,
        "Growth": -46719.00201558,
        "INVESTOR_ID": 5613,
        "PFA_SCHEMEID": 1
    },
    {
        "MONTH_NAME": "Balance as at 07-07-2016",
        "EMPLOYEE_CONTRIBUTION": 3433764.77,
        "EMPLOYER_CONTRIBUTION": 4381387.29,
        "TOTAL_VALUE": 7815152.06,
        "TOTAL_UNITS": 2782788.3885,
        "TOTAL_FEE": 0,
        "TOTAL_CONTRIBUTION": 7815152.06,
        "Voluntary": "0.00"
    },
    {
        "MONTH_NAME": "July 2016",
        "EMPLOYEE_CONTRIBUTION": 0,
        "EMPLOYER_CONTRIBUTION": 0,
        "TOTAL_VALUE": 0,
        "TOTAL_UNITS": -20703.2713,
        "TOTAL_FEE": 0,
        "TOTAL_CONTRIBUTION": 0,
        "Voluntary": "0.00"
    }
]

How to convert it into JSON? Please help me.

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
Maulik Pandya
  • 2,200
  • 17
  • 26

5 Answers5

0

Try this:

NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
0
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:&error];
Sabby
  • 403
  • 3
  • 15
0

JSON is nothing but pairs of key/value pairs enclosed with an enclosed {} braces but you're missing one. You need one more enclosing root {} curly braces and specify key for your array, currently I've specified response, as per your requirement, then the JSON is valid.

{
    "response": [
     ...
    ]   
}

and then you can convert it with what @SaintThread has mentioned.

ldindu
  • 4,270
  • 2
  • 20
  • 24
0

It is already in JSON format.I tried to convert string to json.I got it.But the result is same as what it is in question.

When convert string to json it should be in \"Key\":\"Value\" or \"Key\":Value

NSString *str=@"[{\"SCHEME_NAME\":\"FUG RSA SCHEME\",\"Investment_Value\":-46719.00201558,\"Bid_Price\":2.2566,\"Bid_Price\":\"2.2566\",\"Total_Contributions\":0,\"Growth\":-46719.00201558,\"INVESTOR_ID\":5613,\"PFA_SCHEMEID\":1},{ \"MONTH_NAME\": \"Balance as at 07-07-2016\",\"EMPLOYEE_CONTRIBUTION\": 3433764.77,\"EMPLOYER_CONTRIBUTION\": 4381387.29,\"TOTAL_VALUE\": 7815152.06,\"TOTAL_UNITS\": 2782788.3885,\"TOTAL_FEE\": 0,\"TOTAL_CONTRIBUTION\": 7815152.06,\"Voluntary\": \"0.00\"},{\"MONTH_NAME\": \"July 2016\",\"EMPLOYEE_CONTRIBUTION\": 0,\"EMPLOYER_CONTRIBUTION\":0,\"TOTAL_VALUE\":0,\"TOTAL_UNITS\": -20703.2713,\"TOTAL_FEE\": 0,\"TOTAL_CONTRIBUTION\": 0,\"Voluntary\": \"0.00\"}]";
NSData *dataStr = [str dataUsingEncoding:NSUTF8StringEncoding];
id jsonData = [NSJSONSerialization JSONObjectWithData:dataStr options:0 error:nil];
NSLog(@"The converted string to json is %@",jsonData);

Now the Printed result is

The converted string to json is (
    {
    "Bid_Price" = "2.2566";
    Growth = "-46719.00201558";
    "INVESTOR_ID" = 5613;
    "Investment_Value" = "-46719.00201558";
    "PFA_SCHEMEID" = 1;
    "SCHEME_NAME" = "FUG RSA SCHEME";
    "Total_Contributions" = 0;
},
    {
    "EMPLOYEE_CONTRIBUTION" = "3433764.77";
    "EMPLOYER_CONTRIBUTION" = "4381387.29";
    "MONTH_NAME" = "Balance as at 07-07-2016";
    "TOTAL_CONTRIBUTION" = "7815152.06";
    "TOTAL_FEE" = 0;
    "TOTAL_UNITS" = "2782788.3885";
    "TOTAL_VALUE" = "7815152.06";
    Voluntary = "0.00";
},
    {
    "EMPLOYEE_CONTRIBUTION" = 0;
    "EMPLOYER_CONTRIBUTION" = 0;
    "MONTH_NAME" = "July 2016";
    "TOTAL_CONTRIBUTION" = 0;
    "TOTAL_FEE" = 0;
    "TOTAL_UNITS" = "-20703.2713";
    "TOTAL_VALUE" = 0;
    Voluntary = "0.00";
}
)

Then When I get the data into array

NSArray *arrJson = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
NSLog(@"The arrjson is %@",arrJson);

The printed result is

The arrjson is (
    {
    "Bid_Price" = "2.2566";
    Growth = "-46719.00201558";
    "INVESTOR_ID" = 5613;
    "Investment_Value" = "-46719.00201558";
    "PFA_SCHEMEID" = 1;
    "SCHEME_NAME" = "FUG RSA SCHEME";
    "Total_Contributions" = 0;
},
    {
    "EMPLOYEE_CONTRIBUTION" = "3433764.77";
    "EMPLOYER_CONTRIBUTION" = "4381387.29";
    "MONTH_NAME" = "Balance as at 07-07-2016";
    "TOTAL_CONTRIBUTION" = "7815152.06";
    "TOTAL_FEE" = 0;
    "TOTAL_UNITS" = "2782788.3885";
    "TOTAL_VALUE" = "7815152.06";
    Voluntary = "0.00";
},
    {
    "EMPLOYEE_CONTRIBUTION" = 0;
    "EMPLOYER_CONTRIBUTION" = 0;
    "MONTH_NAME" = "July 2016";
    "TOTAL_CONTRIBUTION" = 0;
    "TOTAL_FEE" = 0;
    "TOTAL_UNITS" = "-20703.2713";
    "TOTAL_VALUE" = 0;
    Voluntary = "0.00";
}
)

Converting NSString to JSON

datasting to json

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
-2

try this online json viewer:

http://jsonviewer.stack.hu/

You can format and view it online

Here is a format

    [
  {
    "SCHEME_NAME": "FUG RSA SCHEME",
    "Investment_Value": -46719.00201558,
    "Bid_Price": 2.2566,
    "Total_Contributions": 0.00,
    "Growth": -46719.00201558,
    "INVESTOR_ID": 5613,
    "PFA_SCHEMEID": 1
  }
,
  {
    "MONTH_NAME": "Balance as at 07-07-2016",
    "EMPLOYEE_CONTRIBUTION": 3433764.77,
    "EMPLOYER_CONTRIBUTION": 4381387.29,
    "TOTAL_VALUE": 7815152.06,
    "TOTAL_UNITS": 2782788.3885,
    "TOTAL_FEE": 0.00,
    "TOTAL_CONTRIBUTION": 7815152.06,
    "Voluntary": "0.00"
  },
  {
    "MONTH_NAME": "July 2016",
    "EMPLOYEE_CONTRIBUTION": 0.00,
    "EMPLOYER_CONTRIBUTION": 0.00,
    "TOTAL_VALUE": 0.00,
    "TOTAL_UNITS": -20703.2713,
    "TOTAL_FEE": 0.00,
    "TOTAL_CONTRIBUTION": 0.00,
    "Voluntary": "0.00"
  }
]
marjohn
  • 7
  • 4