JSON parsing of Android development

Source: Internet
Author: User

is currently trying to write apps, found to understand the code and can write out the gap is very large, the most critical is the Java base comparison of the poor, because only the Python,java Foundation only a week after learning. It is not as straightforward as python to feel the code written in Java.

It's nonsense. Now the development app will definitely involve the interaction with the server, the current preferred is JSON. The use of the parsing JSON package can choose Gson,fastjson and so on.

Now the analysis of a bug that I have been stumped for several days, is the JSON parsing.

Use of the api:http://fanyi.youdao.com/openapi.do?keyfrom=xxx&key=xxx&type=data&doctype=json&version= 1.1&q=good

The JSON obtained:

1 {2"Translation": [3Good4     ],5"Basic": {6"Us-phonetic": "ɡ?d",7"Phonetic": "G?d",8"Uk-phonetic": "G?d",9"explains": [Ten"N. benefits; good deeds; generous deeds", One"Adj. Good, fine, pleasant, pious.", A"Adv. Good", -"N. (good) name; (English); (Sweden) -         ] the     }, -"Query": "Good", -"ErrorCode": 0, -"Web": [ +         { -"Value": [ +Good, AGoods, atGood -             ], -"Key": "Good" -         }, -         { -"Value": [ in"The Crucifixion of Jesus", -"The day of the Crucifixion", to"Passion Day" +             ], -"Key": "Good Friday" the         }, *         { $"Value": [Panax Notoginseng"Sue Good", -"Fantastic", the"Show Silk Mann" +             ], A"Key": "So Good" the         } +     ] -}

By observing JSON discovery, there are both curly braces and brackets.

Paste a snippet of Python parsing JSON code:

1payload={'Keyfrom':'Flaskwechat','Key':'658077913','type':'Data','DOCTYPE':'JSON','version':'1.1','Q':"'}2payload['Q']=content3Url='http://fanyi.youdao.com/openapi.do'4R=requests.get (url,params=payload)5res=R.json ()6     ifres['ErrorCode']==20:7         return 'the text to be translated is too long \ n'8     elifres['ErrorCode']==30:9         return 'Unable to make a valid translation \ n'Ten     elifres['ErrorCode']==40: One         return 'Unsupported language type \ n' A     elifres['ErrorCode']==60: -         return 'No dictionary results \ n' -     Else: theinputkey=res['Query']+'\ n' -Youdaotranslation=u'---youdao translation---'+'\ n'+res['translation'][0]+'\ n' -X=u'---network interpretation---'+'\ n' -         ifRes.has_key ('Basic'): +              forIinchres['Basic'].keys (): -                 ifi=='us-phonetic': +Youdaoweb=x+u'American Reading method:'+res['Basic'][i]+u'(Sorry, only phonetic transcription, really no language)'+'\ n' A                 elifi=='explains': at                      forAinchres['Basic'][i]: -youdaoweb=x+'\ n'-am'\ n'
res[' translation '][0] will be able to get the value of ' translation ', simple and straightforward.
But in Android, Gson parsing, you need to first object, the list of points clear.
Need to first realistic body class, and then encountered {}, this is the object, encountered the [], this is an array, gradually parse


JSON parsing of Android development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.