JSON online translation tool:
http://www.bejson.com/jsoneditoronline/
The first:
First download the required function library, I am using google-gson-2.2.4 here (download link: http://download.csdn.net/detail/a771948524/6668573 ). After the download is complete, create a new lib file on the project, copy the downloaded file into it, right-click and select Add to Build Path. So far, the preparation work is basically completed.
The next step is to write code. Here, I paste the code as follows:
- import com.google.gson.JsonArray;
- import com.google.gson.JsonObject;
- /**
- * @date 2015-05-25
- * @author jack
- *
- */
- publicclass CreateJson {
- publicstaticvoid main(String[] args) {
- //Create a new json object and add properties directly
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("cat", "it");
- //Create a json array, which is the data of one or more groups of json objects
- JsonArray jsonArray = new JsonArray();
- JsonObject jObject1 = new JsonObject();
- jObject1.addProperty("id", 1);
- jObject1.addProperty("name", "java");
- jObject1.addProperty("ide", "Eclipse");
- jsonArray.add(jObject1);
- JsonObject jObject2 = new JsonObject();
- jObject2.addProperty("id", 2);
- jObject2.addProperty("name", "Swift");
- jObject2.addProperty("ide", "X-code");
- jsonArray.add(jObject2);
- JsonObject jObject3 = new JsonObject();
- jObject3.addProperty("id", 3);
- jObject3.addProperty("name", "C#");
- jObject3.addProperty("ide", "Visual Studio");
- jsonArray.add(jObject3);
- jsonObject.add("languages", jsonArray);
- jsonObject.addProperty("pop", "true");
- System.out.println(jsonObject.toString());
- }
- }
import com.google.gson.JsonArray; import com.google.gson.JsonObject; /** * @date 2015-05-25 * @author jack * */ public class CreateJson { public static void main(String[] args) { //Create a new json object and add properties directly JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("cat", "it"); //Create a json array, which is the data of one or more groups of json objects JsonArray jsonArray = new JsonArray(); JsonObject jObject1 = new JsonObject(); jObject1.addProperty("id", 1); jObject1.addProperty("name", "java"); jObject1.addProperty("ide", "Eclipse"); jsonArray.add(jObject1); JsonObject jObject2 = new JsonObject(); jObject2.addProperty("id", 2); jObject2.addProperty("name", "Swift"); jObject2.addProperty("ide", "X-code"); jsonArray.add(jObject2); JsonObject jObject3 = new JsonObject(); jObject3.addProperty("id", 3); jObject3.addProperty("name", "C#"); jObject3.addProperty("ide", "Visual Studio"); jsonArray.add(jObject3); jsonObject.add("languages", jsonArray); jsonObject.addProperty("pop", "true"); System.out.println(jsonObject.toString()); } }
The second:
java json string to JSONObject and JSONArray and value
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
- public class JsonTest {
- public static void main(String[] args) {
- String joStr = "{name:\"张三\",age:\"20\"}";
- //Convert json string to JSONObject
- JSONObject jsonObject = JSONObject.fromObject(joStr);
- //Get the information inside through getString("") respectively
- String name = jsonObject.getString("name");
- String age = jsonObject.getString("age");
- //output Zhang San 20
- System.out.println(name+" "+age);
- String jaStr = "[{user:{name:\"张三\",age:\"20\"}},{score:{yuwen:\"80\",shuxue:\"90\"}}]";
- //Convert jsonArray string to JSONArray
- JSONArray jsonArray = JSONArray.fromObject(jaStr);
- // get the first element of the array
- JSONObject jUser = jsonArray.getJSONObject(0).getJSONObject("user");
- //Take out the information of the first element and convert it to JSONObject
- String name2 = jUser.getString("name");
- String age2 = jUser.getString("age");
- //output Zhang San 20
- System.out.println(name2+" "+age2);
- //Take out the second element of the array and convert it to JSONObject
- JSONObject jScore = jsonArray.getJSONObject(1).getJSONObject("score");
- //Retrieve the information of the second element
- String yuwen = jScore.getString("yuwen");
- String shuxue = jScore.getString("shuxue");
- // output 80 90
- System.out.println(yuwen+" "+shuxue);
- }
- }
The third:
Jackson is a class library that Java uses to process data in JSON format with very good performance.
Example:
{ : { : , : }, : , : , : }
class User {
Gender { MALE, FEMALE };
class Name {
_first, _last;
getFirst() { _first; }
getLast() { _last; }
void setFirst( s) { _first = s; }
void setLast( s) { _last = s; }
}
Gender _gender;
Name _name;
_isVerified;
[] _userImage;
Name getName() { _name; }
isVerified () {_isVerified; }
Gender getGender() { _gender; }
[] getUserImage () {_userImage; }}
void setName(Name n) { _name = n; }
void setVerified( b) { _isVerified = b; }
void setGender(Gender g) { _gender = g; }
void setUserImage([] b) { _userImage = b; }
}
ObjectMapper mapper = ObjectMapper(); User user = mapper.readValue( File(), User.class);