“Convertir l'objet Java en JSON” Réponses codées

Objet JavaScript à JSON

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person); 
Grepper

Convertir la chaîne JSON en objet JSON en Java

try {
     JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}
Lucky LyreDragonbird

Java s'oppose à JSON

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.ObjectWriter; 

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
Talented Trout

objet JSON à Java

String json = "...";
ObjectMapper m = new ObjectMapper();
Set<Product> products = m.readValue(json, new TypeReference<Set<Product>>() {});
Pleasant Puma

Convertir l'objet Java en JSON

User u = new User();
u.firstName = "Sample";
u.lastName = "User";
u.email = "[email protected]";

ObjectMapper mapper = new ObjectMapper();
    
try {
    // convert user object to json string and return it 
    return mapper.writeValueAsString(u);
}
catch (JsonGenerationException | JsonMappingException  e) {
    // catch various errors
    e.printStackTrace();
}
Cooperative Chimpanzee

Convertir l'objet Java en JSON

public class Student {
	
	Integer id;
	Map<String,Integer> marks;
	List<Address> addresses;
	
	public Student(Integer id, Map<String, Integer> marks, List<Address> addresses) {
		this.id = id;
		this.marks = marks;
		this.addresses = addresses;
	}
}

class Address {
	
	String addrType;
	Integer houseNo;
	String streetName;
	String countryName;
	House house;
	
	public Address(String addrType, Integer houseNo, String streetName, String countryName, House house) {
		this.addrType = addrType;
		this.houseNo = houseNo;
		this.streetName = streetName;
		this.countryName = countryName;
		this.house = house;
	}	
}

class House {
	
	Integer noOfRooms;
	String houseType;
	Integer noOfWindows;
	
	public House(Integer noOfRooms, String houseType, Integer noOfWindows) {
		this.noOfRooms = noOfRooms;
		this.houseType = houseType;
		this.noOfWindows = noOfWindows;
	}
}
ABHIJEET K BEHERA

Réponses similaires à “Convertir l'objet Java en JSON”

Questions similaires à “Convertir l'objet Java en JSON”

Plus de réponses similaires à “Convertir l'objet Java en JSON” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code