Home > Back-end >  Consult with JAVA parse Scott map inverse method of address decoding JSON, I write is a problem with
Consult with JAVA parse Scott map inverse method of address decoding JSON, I write is a problem with

Time:10-06

Recent analysis learning a JAVA open source project source code, but stuck in gold parsing the JSON, not wrong is to get an empty string, here to consult everybody friends, see me how to change the code, thank you, JSON content as follows:
 {
"Status" : "1",
"Regeocode" : {
"AddressComponent" : {
"City", "wuhan",
"Province", "hubei province",
"Adcode" : "420106",
"District", "wuchang district",
"Towncode" : "420106006000",
"StreetNumber" : {
"Number" : "no. 46,"
"Location" : "114.300143, 30.5515561,"
"Direction" : "the northeast,"
"Short" : "32.3008",
"Street" : "stone bridge"
},
"Country" : "China,"
"Township" : "austral street street,"
"BusinessAreas" : [
{
"Location" : "114.300581, 30.547575,"
"Name" : "the door",
"Id" : "420106"
},
{
"Location" : "114.305741, 30.583179,"
"Name" : "river beach,"
"Id" : "420102"
},
{
"Location" : "114.31082, 30.560542,"
"Name" : "" jade bridge,
"Id" : "420106"
}
],
"Building" : {
"Name" : [

],
"Type" : [

]
},
"Neighborhood" : {
"Name" : [

],
"Type" : [

]
},
"Citycode" : "027"
},
"Formatted_address" : "wuchang district in wuhan city, hubei province silver poly austral street street garden district in the southern district of wuchang honest culture park"
},
"Info" : "OK,"
"Infocode" : "10000"
}

I write the JAVA functions as follows:
 
Public Address parseAddress (JsonObject json) {
If (json. Get string (" status ".) the equals (" 1 ")) {
JsonObject regecode=json. GetJsonObject (" regeocode ");
If (regecode!=null) {
Address the Address=new Address ();

If (regecode. Either containsKey (" formatted_address ")) {
JsonArray format_address=json. GetJsonObject (" regeocode "). GetJsonArray (" formatted_address ");
Address. SetFormattedAddress (format_address. ToString ());
}
JsonObject result=regecode. GetJsonObject (" addressComponent ");

If (result!=null) {
If (result. Either containsKey (" adcode "))
Address. SetPostcode (result. GetJsonArray (" adcode "). The toString ());

If (result. Either containsKey (" city "))
Address. SetSettlement (result. GetJsonArray (" city "). The toString ());

If (result. Either containsKey (" province "))
Address. SetState (result. GetJsonArray (" province "). The toString ());

If (result. Either containsKey (" district "))
Address. SetDistrict (result. GetJsonArray (" district "). The toString ());

If (result. Either containsKey (" county "))
Address. SetCountry (result. GetJsonArray (" county "). The toString ());

StreetNumber JsonObject location=result. GetJsonObject (" ");
If (the location!=null) {
If (location. Either containsKey (" street "))
Address. SetStreet (location. GetJsonArray (" street "). The toString ());

If (location. Either containsKey (" number "))
Address. SetHouse (location. GetJsonArray (" number "). The toString ());
}
The return address.
}
}
}
return null;
}

Trouble friends to help me correction, thank you.

CodePudding user response:

Emmmmmm, third-party JSON parsing class not sweet ~?

CodePudding user response:

Public class GaoDeStruct {
Private String status;
Private String info;
Private String infocode;
Private Regeocode Regeocode;
//the getter his
//setter yourself added

Public static void main (String [] args) {
The json String String json="you"

AddressComponent address=parse (json);
//if you compare the printed information as it means the right original json string
System. The out. Println (JSONObject. ToJSONString (address));
}

Public static AddressComponent parse (String json) {
GaoDeStruct struct=JSONObject. ParseObject (json, GaoDeStruct. Class);
Return struct. GetRegeocode (.) getAddressComponent ();
}
}

The class Regeocode {
Private AddressComponent AddressComponent;
Private String formatted_address;
//the getter his
//setter yourself added
}

The class AddressComponent {
Private String city;
Private String province;
Private String adcode;
Private String district;
Private String towncode;
Private StreetNumber StreetNumber;
Private String country;
Private String township;
Private List BusinessAreas;
Private Building Building;
Private Neighborhood Neighborhood;
Private String citycode;
//the getter his
//setter yourself added
}

The class StreetNumber {
Private String number;
Private String location;
Private String direction;
Private String short;
Private String street;
//the getter his
//setter yourself added
}

The class BusinessArea {
Private String location;
private String name;
private String id;
//the getter his
//setter yourself added
}

The class Building {
Private List The name;
Private List The type;
//the getter his
//setter yourself added
}

The class Neighborhood {
Private List The name;
Private List The type;
//the getter his
//setter yourself added
}

Really don't understand why now make json how so like jsonObject. Through the get (property names) way to explain the json string, json with class can be mutual transformation, namely originally the json string class want to express written, no?

CodePudding user response:

nullnullnullnullnullnullnullnull
  • Related