I do a client on android http client to accept the return data. Get a String mess, the JSONobject.toString get.
Import java json common package (6) afterandroid parsing code as follows:
String mess = "{" loginuser ": [{" password ": " 123 ", " person ": {" address & quot;: " 4654 ", " id ": 1, " name ": " ttjxffda ", " phone ": " 1212 "}, " status ": 0, " username ": " ttjxtjx "}], " loginresult ": " success "} ";
JSONObject jsonObject = JSONObject.fromObject (mess);
String state = (String) jsonObject.get ("loginresult");
if (state.equals ("success")) {
JSONArray jsonArray = jsonObject.getJSONArray ("loginuser");
for (int i = 0; i
Libuser user = new Libuser ();
user.setUsername (item.getString ("username"));
user.setPassword (item.getString ("password"));
user.setStatus (item.getInt ("status"));
}
}
Unit Testing Times in android exception java.lang.IllegalArgumentException: already added: Lorg / apache / commons / collections / ArrayStack; project can not go wrong test
In java project through testing.
I know this is because of the introduction of duplicate packets, I deleted the commons-collections.jar (3.2) this package, the results reported could not find class exception:
case by: NoClassDefFoundError org.apache.commons.map.listOrderedMap.
Can not be resolved. Tangle. Seek expert advice
I have had the common sense that comes with android json class, but it will not mess parsing strings,
org.json.JSONException: Expected ':' after & quot at character 7 of {" loginuser ": [{" password ": " 123 ", & quot; person ": {" address ": " 4654 ", " id ": 1, " name ": " ttjxffda ", " phone ": " 1212 "}, " status ": 0, " username ": " ttjxtjx & quot;}], " loginresult ": " success "}
at org.json.JSONTokener.syntaxError (JSONTokener.java: 446)
at org.json.JSONTokener.readObject (JSONTokener.java: 375)
at org.json.JSONTokener.nextValue (JSONTokener.java: 96)
at org.json.JSONObject
at org.json.JSONObject
at cec.jmu.test.LoginTest.getLoginstate (LoginTest.java: 38)
at java.lang.reflect.Method.invokeNative (Native Method)
at android.test.AndroidTestRunner.runTest (AndroidTestRunner.java: 169)
at android.test.AndroidTestRunner.runTest (AndroidTestRunner.java: 154)
at android.test.InstrumentationTestRunner.onStart (InstrumentationTestRunner.java: 520)
at android.app.Instrumentation $ InstrumentationThread.run (Instrumentation.java: 1447)
mess content code has. outprint result: {"loginuser": [{"password": "123", "person": {"address": "4654", "id": 1, "name": "ttjxffda", "phone": " 1212 "}," status ": 0," username ":" ttjxtjx "}]," loginresult ":" success "}
The code generated mess, Libuser There is a Person object.
Libuser loginuser = LibuserService.getLoginUser (user);
System.out.println (loginuser.toString ());
JSONObject jsonObject = new JSONObject ();
JSONObject item = new JSONObject ();
JSONArray jsonArray = new JSONArray ();
item.put ("username", loginuser.getUsername ());
item.put ("password", loginuser.getPassword ());
item.put ("status", loginuser.getStatus ());
jsonArray.element (loginuser);
jsonObject.put ("loginuser", jsonArray);
jsonObject.put ("loginresult", loginresult);
this.message = jsonObject.toString ();
System.out.println (message);
Their own investigation for a long time without a solution. Tangled ah, seeking expert advice.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Code Description Everything
public void json () {
String str = "{\" loginuser \ ": [{\" password \ ": \" 123 \ ", \" person \ ": {\" address \ ": \" 4654 \ ", \" id \ ": 1, \ "name \": \ "ttjxffda \", \ "phone \": \ "1212 \"}, \ "status \": 0, \ "username \": \ "ttjxtjx \"}], \ "loginresult \": \ "success \"} ";
try {
JSONObject jsonObject = new JSONObject (str);
String loginresult = jsonObject.getString ("loginresult");
System.out.println (loginresult);
if (loginresult.equals ("success")) {
JSONArray jsonArray = jsonObject.getJSONArray ("loginuser");
JSONObject jsonObjectData = (JSONObject) jsonArray.opt (0);
String password = jsonObjectData.getString ("password");
String status = jsonObjectData.getString ("status");
String username = jsonObjectData.getString ("username");
System.out.println (password);
System.out.println (status);
System.out.println (username);
JSONObject jsonObjectPerson = jsonObjectData.getJSONObject ("person");
String address = jsonObjectPerson.getString ("address");
String id = jsonObjectPerson.getString ("id");
String name = jsonObjectPerson.getString ("name");
String phone = jsonObjectPerson.getString ("phone");
System.out.println (address);
System.out.println (id);
System.out.println (name);
System.out.println (phone);
}
} Catch (Exception e) {
/ / TODO: handle exception
}
}
03-23 14:29:02.946: I / System.out (22411): success
03-23 14:29:02.946: I / System.out (22411): 123
03-23 14:29:02.946: I / System.out (22411): 0
03-23 14:29:02.946: I / System.out (22411): ttjxtjx
03-23 14:29:02.946: I / System.out (22411): 4654
03-23 14:29:02.946: I / System.out (22411): 1
03-23 14:29:02.946: I / System.out (22411): ttjxffda
03-23 14:29:02.946: I / System.out (22411): 1212
03-23 14:29:02.946: I / System.out (22411): onResume
Reply:
import org.json.JSONArray;
import org.json.JSONObject;
public void json () {
String str = "{\" loginuser \ ": [{\" password \ ": \" 123 \ ", \" person \ ": {\" address \ ": \" 4654 \ ", \" id \ ": 1, \ "name \": \ "ttjxffda \", \ "phone \": \ "1212 \"}, \ "status \": 0, \ "username \": \ "ttjxtjx \"}], \ "loginresult \": \ "success \"} ";
try {
JSONObject jsonObject = new JSONObject (str);
String loginresult = jsonObject.getString ("loginresult");
System.out.println (loginresult);
if (loginresult.equals ("success")) {
JSONArray jsonArray = jsonObject.getJSONArray ("loginuser");
JSONObject jsonObjectData = (JSONObject) jsonArray.opt (0);
String password = jsonObjectData.getString ("password");
String status = jsonObjectData.getString ("status");
String username = jsonObjectData.getString ("username");
System.out.println (password);
System.out.println (status);
System.out.println (username);
JSONObject jsonObjectPerson = jsonObjectData.getJSONObject ("person");
String address = jsonObjectPerson.getString ("address");
String id = jsonObjectPerson.getString ("id");
String name = jsonObjectPerson.getString ("name");
String phone = jsonObjectPerson.getString ("phone");
System.out.println (address);
System.out.println (id);
System.out.println (name);
System.out.println (phone);
}
} Catch (Exception e) {
/ / TODO: handle exception
}
}
Reply:
Thank resolved, and double quotes I got through the web page where the data String mess is all represented by the escape character. . . So when new jsonobject error. . . . Khan trouble you. . .
Reply:
Json android comes with the package on the line. . But android2.2 and java json package conflicts. . . Not find a solution
Reply:
I also encountered the same problem, neighborhoods landlord, double escape character represents is how to convert out
Reply:
Asked the same escape character is how to replace the double quotes?
Reply:
Landlord it? With seeking solutions.
No comments:
Post a Comment