简体   繁体   中英

Java Objective-C Json data

I have a little problem with understanding this too lines of code in Objective-C :

id jsonData;
NSInteger errorCode;
NSString* errorString;
errorCode = [jsonData sqlInt:@"error_code"];
errorString = [[jsonData objectForKey:@"error_string"] retain];

I'm trying to find a way how to do the same code in Java,that's why I need a little help to understand this piece of code. Thanks in advance!!!

EDIT: Is this code do the same as in Objective C? :

 ErrorCode = new JSONObject();
    ErrorCode.put("error_code", errorCode);
    ErrorString = new JSONObject();
    ErrorString.put("error_string", errorString);

Try this ,

SBJSON *jsonData = [SBJSON alloc]init]; - Objective c

private JSONObject jObject; - Java

// you get a JSON Data - Objective C

NSDictionary *errorString = [[jsonData objectForKey:@"error_string"] retain];

For Java -

jObject = new JSONObject(errorString); 

JSONArray menuitemArray = popupObject.getJSONArray("error_string");

String attributeValue = jObject.getString("value");

System.out.println(attributeValue);

Presumably your jsonData is in a dictionary of some sort, and presumably sqlInt is either a "category" extension to NSDictionary or a method of some custom dictionary class that is being used by the JSON parser. From its name it takes a named element of some sort and returns its int representation.

So you have two elements in your dictionary, one named "error_code" and one named "error_string". "error_string" can simply be extracted into a String, but "error_code" is either an Integer object or a String representation of an integer -- can't tell which from your code (but you can tell by looking at the source JSON -- An Integer value will NOT be surrounded by quotes, but a String value will).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM