Use Java-jsonarray

Source: Internet
Author: User
  1. Java --- use jsonarray

  2. Public modelandview ajaxtest (httpservletrequest request,
  3. Httpservletresponse response) throws exception {
  4. Response. setcharacterencoding ("UTF-8 ");
  5. String data = request. getparameter ("data ");
  6. Log.info (">>>>>>>> DATA =" + data );
  7. Try {
  8. Jsonobject jsonobj = new jsonobject (data );
  9. Jsonarray jarr = (jsonarray) jsonobj. Get ("data ");
  10. For (INT I = 0; I <jarr. Length (); I ++ ){
  11. Jsonobject jobj = (jsonobject) jarr. Get (I );
  12. String name = NULL;
  13. String value = NULL;
  14. Try {
  15. Name = (string) jobj. Get ("name ");
  16. Value = (string) jobj. Get ("value ");
  17. Log.info (">>>>" + name + "=" + value );
  18. } Catch (exception Jex ){}
  19. }
  20. Response. getwriter (). Write ("{success: True, Info: '" +
  21. Stringescapeutils. escapejavascript ("saved successfully! ") + "'}");
  22. } Catch (exception e ){
  23. Response. getwriter (). Write ("{success: false, Info: '" +
  24. Stringescapeutils. escapejavascript (E. getmessage () + "'}");
  25. }
  26. Return NULL;
  27. }

Related conversions:

Public static jsonarray getjsonarrayfromresource (context, int resourceid)

Throws jsonexception, ioexception
{
Inputstream is = context. getresources (). openrawresource (resourceid );
Return new jsonarray (utility. getstringfrominputstream (is ));
}

Public static jsonobject getjsonobjectfromresource (context, int resourceid) throws jsonexception, ioexception {
Inputstream is = context. getresources (). openrawresource (resourceid );
Return new jsonobject (utility. getstringfrominputstream (is ));
}

Public static jsonarray getjsonarrayfromstring (string jsonarraystring) throws jsonexception {
Return new jsonarray (jsonarraystring );
}

Public static jsonobject getjsonobjectfromstring (string jsonstring) throws jsonexception
{
Return new jsonobject (jsonstring );
}

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.