Data exchange between JSON and Java objects

Source: Internet
Author: User

// Www.json.com/google-gson-2.1jar is recommended.

Package com. Jason. servlet;

Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;
Import java. util. Map;
Import java. util. Map. entry;
Import java. util. Set;

Import com. Google. gson. gson;
Import com. Google. gson. jsonelement;
Import com. Google. gson. jsonobject;
Import com. Google. gson. jsonparser;

Public class testdemo
{
Public static void main (string [] ARGs)
{
String STR = "{\" device sales process \ ": \" koId-001 \ ", \" consumables sales process \ ": \" koId-002 \"}";
Jsonparser JP = new jsonparser ();
Jsonelement je = JP. parse (STR); // parse the Jason string in Java
Jsonobject Jo = je. getasjsonobject ();
Set <entry <string, jsonelement> set = Jo. entryset ();
For (iterator <entry <string, jsonelement> iter = set. iterator (); ITER
. Hasnext ();)
{
Entry <string, jsonelement> E = ITER. Next ();
System. Out. println (E. getkey () + "," + E. getvalue ());
}
Gson = new gson ();
// Operate Java objects

Worktodo W1 = new worktodo ();
W1.setid ("process ID-001 ");
W1.getitemmap (). Put ("job ID-001", "Start ");
W1.getitemmap (). Put ("job ID-002", "end ");

Worktodo W2 = new worktodo ();
W2.setid ("process ID-002 ");
W2.getitemmap (). Put ("job ID-001", "Start ");
W2.getitemmap (). Put ("job ID-002", "end ");

PROCESS p = new process ();
P. setproname ("device sales process ");
P. setkoid ("koId-001 ");
List <worktodo> Ws = new arraylist <worktodo> ();
WS. Add (W1 );
WS. Add (W2 );
P. setworktodolist (WS );
Int COUNT = 0;
For (worktodo W: WS)
{
Count + = W. getcount ();
}
P. setcount (count );

System. Out. println (gson. tojson (p ));
}
}

Class Process
{
Private string proname;
Private string koid;
Private int count;
Private list <worktodo> worktodolist = new arraylist <worktodo> ();

Public process ()
{

}

Public process (string koid, list <worktodo> worktodolist)
{
This. koid = koid;
This. worktodolist = worktodolist;
}

Public String getkoid ()
{
Return koid;
}

Public void setkoid (string koid)
{
This. koid = koid;
}

Public list <worktodo> getworktodolist ()
{
Return worktodolist;
}

Public void setworktodolist (list <worktodo> worktodolist)
{
This. worktodolist = worktodolist;
}

Public int getcount ()
{
Return count;
}

Public void setcount (INT count)
{
This. Count = count;
}

Public String getproname ()
{
Return proname;
}

Public void setproname (string proname)
{
This. proname = proname;
}

}

Class worktodo
{
Private string ID;
Private Map <string, string> itemmap = new hashmap <string, string> ();

Public worktodo ()
{
}

Public String GETID ()
{
Return ID;
}

Public void setid (string ID)
{
This. ID = ID;
}

Public Map <string, string> getitemmap ()
{
Return itemmap;
}

Public void setitemmap (Map <string, string> itemmap)
{
This. itemmap = itemmap;
}

Public int getcount ()
{
Return this. itemmap. Size ();
}

}

Output: device sales process, "koId-001"
Consumables sales process, "koId-002"
{"Proname": "device sales process", "koid": "koId-001", "Count": 4, "worktodolist": [{"ID ": "process ID-001", "itemmap": {"job ID-002": "end", "job ID-001": "Start "}}, {"ID": "process ID-002", "itemmap": {"job ID-002": "end", "job ID-001 ": "Start" }}]}

// Ajax Data Transmission

VaR XMLHttpRequest;
Function ajaxtest ()
{
If (window. activexobject)
XMLHttpRequest = new activexobject ("Microsoft. XMLHTTP ");
// XMLHttpRequest = new activexobject ("msxml2.xmlhttp"); // suitable for new ie versions, but the above can also be used
Else if (window. XMLHttpRequest)
XMLHttpRequest = new XMLHttpRequest ();

If (XMLHttpRequest! = "Undefined ")
// If ("undefined "! = XMLHttpRequest) // do not recommend this method. It may be incorrect in IE.
{
// Prepare data, GET request; url = ajaxservlet; true indicates asynchronous Processing
VaR url = "$ {pagecontext. Request. contextpath}/ajaxservlet ";
// Submit the request in get mode, with parameters following the URL
/* XMLHttpRequest. Open ("get", URL, true );
XMLHttpRequest. onreadystatechange = ajaxcallback; // callback function
XMLHttpRequest. Send (null); // send the request
*/
// Submit in post Mode

XMLHttpRequest. Open ("Post", URL, true );
XMLHttpRequest. onreadystatechange = ajaxcallback; // callback function
VaR JSON = {"001": "A", "002": "B "};
XMLHttpRequest. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); // you must add
XMLHttpRequest. Send ("JSON =" + JSON. stringify (JSON); // send a request
}

}

Function ajaxcallback ()
{
If (XMLHttpRequest. readystate = 4)
{
If (XMLHttpRequest. Status = 200)
{
VaR JSON = eval ('+ XMLHttpRequest. responsetext + ')');
For (var I in JSON)
{
Alert (I + "," + JSON [I]);
}
}
}
}

 

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.