Using Method.invoke (..) Parsing JSON

Source: Internet
Author: User

Call

Inijsonobject (Person.class.getName (), Createjsonobject ());
IniJSONObject2 (World.class.getName (), CreateJSONObject2 ());

/**
* Just an Ordinary object
*
* @param name
* (package name + class name)
* @return
*/
Private Object Inijsonobject (String name, Jsonobject jsonobject) {
try {
It is wrong to put the class name .....
class<?> forname = class.forname ("person");
Package name + class name, correct
String name2 = Person.class.getName ();
class<?> forname = class.forname (name);
Object newinstance = Forname.newinstance ();
Method[] methods = Forname.getmethods ();
hashmap<string, method> HashMap = new hashmap<string, method> ();
for (int i = 0; i < methods.length; i++) {
String methodName = Methods[i].getname ();
System.out.println (forname + "method list:" + methodName);
Hashmap.put (MethodName, methods[i]);
}
Get a list of key fields for Jsonobject ...
Get a list of key fields for Jsonobject ...
Get a list of key fields for Jsonobject ...
Iterator Iterator = Jsonobject.keys ();
while (Iterator.hasnext ()) {
String key = Iterator.next (). toString ();
Object value = Jsonobject.get (key);
UserName
Userage
Userhobby
System.out.println ("Jsonobject key=" + key);
See if the method list has the Set method
if (Hashmap.containskey ("set" + Wordfirsttoupper (key))) {
method = Hashmap.get ("Set" + Wordfirsttoupper (key));
String methodName = Method.getname ();
String type = Method.getparametertypes () [0].getname ();
Call the method .....
Method.invoke (newinstance, value);
System.out.println ("Call method:" + MethodName + "method's parameter type is:" + type+ "worth the type:" +value.getclass (). GetName ());

}
}
System.out.println ("Analytic result:" + newinstance.tostring ());
return newinstance;
} catch (Jsonexception e) {
E.printstacktrace ();
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
}
return null;
}

/**
* objects are in the object
*
* @param name
* (package name + class name)
* @param jsonobject
*/
private void IniJSONObject2 (String name, Jsonobject jsonobject) {
try {

class<?> forname = class.forname (name);
Object newinstance = Forname.newinstance ();
Get a list of methods for this class
Method[] methods = Forname.getmethods ();
hashmap<string, method> hashMap2 = new hashmap<string, method> ();
for (int i = 0; i < methods.length; i++) {
Hashmap2.put (Methods[i].getname (), methods[i]);
}
Iterator keys = Jsonobject.keys ();
while (Keys.hasnext ()) {
The key field ......
String key = Keys.next (). toString ();
Value
Object object = Jsonobject.get (key);
if (Hashmap2.containskey ("set" + Wordfirsttoupper (key))) {
Gets the parameter type of the Set method
method = Hashmap2.get ("Set" + Wordfirsttoupper (key));
Type of parameter (package name + class name)
String type = Method.getparametertypes () [0].getname ();
If (object instanceof Jsonobject) {
It's a jsonobject.
Method.invoke (Newinstance,
Inijsonobject (Type, (Jsonobject) object));
} else if (object instanceof Jsonarray) {

} else {
Method.invoke (newinstance, object);
}
SYSTEM.OUT.PRINTLN ("parameter type of method:" +type+ "/Worthwhile type:/" +object.getclass (). GetName ());

}
}
SYSTEM.OUT.PRINTLN ("parsed result:" + newinstance.tostring ());

} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
} catch (Jsonexception e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
}

}

/**
* @param word
* @return First Letter Capital
*/
public static string Wordfirsttoupper (string word) {
int c = Word.charat (0);
if (c >= && C <= 122) {
c-= 32;
// }
Return String.Format ("%c%s", C, word.substring (1));

Return Word.replacefirst (word.substring (0, 1), word.substring (0, 1)
. toUpperCase ());
}

Public Jsonobject Createjsonobject () {
Jsonobject jsonobject = new Jsonobject ();
try {
Jsonobject.put ("UserName", "Wuxifu");
Jsonobject.put ("Userage", 110);
Jsonobject.put ("Userhobby", 1);
} catch (Jsonexception e) {
E.printstacktrace ();
}
return jsonobject;

}

Public Jsonobject CreateJSONObject2 () {
Jsonobject jsonobject = new Jsonobject ();
Jsonobject jsonObject2 = new Jsonobject ();
try {
Jsonobject.put ("person", jsonObject2);
Jsonobject.put ("Nums", 110);
Jsonobject.put ("Ages", 2015);
Jsonobject2.put ("UserName", "Wuxifu");
Jsonobject2.put ("Userage", 110);
Jsonobject2.put ("Userhobby", 1);
} catch (Jsonexception e) {
E.printstacktrace ();
}
return jsonobject;

}

Package Com.example.testviewpager;

public class Person {
Private String UserName;
private int userage;
private int userhobby;

Public person () {
Super ();
TODO auto-generated Constructor stub
}
Public person (String userName, int userage, int userhobby) {
Super ();
This.username = UserName;
This.userage = Userage;
This.userhobby = Userhobby;
}
Public String GetUserName () {
return userName;
}
public void Setusername (String userName) {
This.username = UserName;
}
public int getuserage () {
return userage;
}
public void setuserage (int userage) {
This.userage = Userage;
}
public int Getuserhobby () {
return userhobby;
}
public void Setuserhobby (int userhobby) {
This.userhobby = Userhobby;
}
@Override
Public String toString () {
return "person [username=" + UserName + ", userage=" + userage
+ ", userhobby=" + Userhobby + "]";
}



}

Package Com.example.testviewpager;

public class World {
private person person;
private int nums;
private int ages;

Public World () {
Super ();
}

Public Person Getperson () {
return person;
}

public void Setperson (person person) {
This.person = person;
}

public int getnums () {
return nums;
}

public void setnums (int nums) {
This.nums = Nums;
}

public int getages () {
return ages;
}

public void setages (int ages) {
This.ages = ages;
}

@Override
Public String toString () {
Return "World [person=] + person +", nums= "+ Nums +", ages= "+ Ages
+ "]";
}

}


Using Method.invoke (..) Parsing JSON

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.