How to parse JSON string without using any API?
I need to use the JSON parser in my iPhone application. We have API's which are used to pars开发者_运维技巧e the data. I just want to know, how can we do without using any API ?
Thank you.
- Use the APIs.
- Use the APIs.
- Use the APIs.
They're quick, they've been tested to work, and you don't have to think about them.
If you're still committed to writing a JSON parser (and/or if this is an academic pursuit), then you're likely to benefit from researching the JSON specification and brushing up on [your platform of choice]'s string operations and regular expressions library. Then, as @alexanderb suggests, create a small library of classes and/or functions to support you.
Yes, you can. Since JSON is a simple string, you could create your own parser that is able to serialize string to target object.
For instance, you have JSON like that
{ description: "my description", value: 10 }
You can extract description value, by regex and return object of class, like
class Target
{
string Description;
int value;
}
But, of cause it always a good idea to use time-proven JSON parsers.
Copy the 8k of these 238 lines (+/- comments) into your code:
http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js?r=12
精彩评论