So I’m trying to understand how to smear JSON on the object. What you see in the screenshot is, it is mapping Json with Java object (as I thought). Made by Mkyong.com/java/Jackson-2-Convert-JAVA-COs-To-From-JSON. It seemed to me if I would run Main method, it would pass the result of the response into the CITYLIST variable. CITYLIST object is simply Pojo object. I would understand what the mistake says.
I want a JSON answer via GET request (URL) pass to Java object
here:
import java.net.url;
Import com.fasterxml.jackson.databind.ObjectMapper;
Public Class Jacksonexample2 {
Public Static Void Main (String [] Args) {
OBJECTMAPPER MAPPER = NEW OBJECTMAPPER ();
Try {
URL URL = NEW URL ("https: // someurl");
// JSON File To Java Object
CityList CityList = Mapper.ReadValue (URL, CITYLIST.CLASS);
// Compact Print.
System.out.PrintLN (CityList);
} Catch (IoException E) {
E.PrintStackTrace ();
}
}
}
Simple Pojo object CityList:
package model;
Import java.util.map;
Public Class CityList {
Private Map & Lt; String, String & GT; Plus_Code;
Public Map & Lt; String, String & GT; getplus_code () {
RETURN PLUS_CODE;
}
Public Void Setplus_Code (Map & Lt; String, String & GT; Plus_Code) {
this.plus_code = plus_code;
}
}
Here is the answer JSON, example:
{
"Plus_code": {
"compound_code": "P27Q + MC New York, NY, USA",
"Global_Code": "87G8P27Q + MC"
}
}
Answer 1, Authority 100%
introduced your JSON an example in the form of an object
Public Class CityList {
@JsonProperty ("Plus_Code")
Private Pluscode Pluscode;
Public Pluscode getpluscode () {
Return Pluscode;
}
Public Void Setpluscode (Pluscode Pluscode) {
this.pluscode = pluscode;
}
Public Class Pluscode {
@JsonProperty ("compound_code")
Private String CompoundCode;
@JsonProperty ("Global_Code")
Private String Globalcode;
Public String GetCompoundCode () {
Return compoundcode;
}
Public Void SetCompoundCode (String CompoundCode) {
this..compoundcode = compoundcode;
}
Public String GetGlobalcode () {
RETURN GLOBALCODE;
}
Public Void Setglobalcode (String Globalcode) {
this.globalcode = globalcode;
}
}
}
checked
Public Static Void Main (String [] Args) {
OBJECTMAPPER MAPPER = NEW OBJECTMAPPER ();
String Json = "{\" plus_code \ ": {\" compound_code \ ": \" P27Q + MC New York, NY, USA \ ", \" global_code \ ": \" 87G8P27Q + MC \ "}}";
Try {
// URL URL = NEW URL ("");
// JSON File To Java Object
CITYLIST CITYLIST = MAPPER.REDVALUE (JSON, CITYLIST.CLASS);
// Compact Print.
System.out.PrintLN (cityList.getpluscode (). GetCompoundcode () + "" + cityList.getpluscode (). GetGlobalcode ());
} Catch (IoException E) {
E.PrintStackTrace ();
}
}
and got the desired result