对于以下JSON,我想将地址元素打印到控制台
[
{
"firstName": "Lakshay",
"lastName": "Sharma",
"age": 35,
"emailAddress": "Lakshay@Gmail.com",
"address": {
"streetAddress": "Shalimar Bagh",
"city": "Delhi",
"postCode": "110088",
"state": "Delhi",
"country": "India",
"county": "Delhi"
},
"phoneNumber": {
"home": "012345678",
"mob": "0987654321"
}
},
{
"firstName": "Virender",
"lastName": "Singh",
"age": 35,
"emailAddress": "Virender@Gmail.com",
"address": {
"streetAddress": "Palam Vihar",
"city": "Gurgaon",
"postCode": "122345",
"state": "Haryana",
"country": "India",
"county": "Delhi"
},
"phoneNumber": {
"home": "012345678",
"mob": "0987654321"
}
}
]
这是我的Java类,我存储了JSON键的所有数据类型,并创建了3个类.Class CustomerDataType也包含PhoneNumber和Address类。
package com.practice.SeleniumWebDriver;
public class CustomerDataTypes {
public String firstName;
public String lastName;
public int age;
public String emailAddress;
public Address address;
public PhoneNumber phoneNumber;
public static class Address {
public String streetAddress;
public String city;
public String postCode;
public String state;
public String country;
public String county;
}
public class PhoneNumber {
public String home;
public String mob;
}
}
在我的Main类中,我编写了下面的代码来访问JSON的所有元素,但在获取Address类(它是CustomerDataTypes的内部类)时遇到问题
package com.practice.SeleniumWebDriver;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.practice.SeleniumWebDriver.CustomerDataTypes.Address;
public class ReadJsonData {
private static List<CustomerDataTypes> customerList;
public static List<CustomerDataTypes.Address> CustomerAddress;
//private static List<CustomerDataTypes.PhoneNumber> CustomerPhoneNumber;
public static String CustomerFilePath="D:\\eclipse-workspace\\SeleniumWebDriver\\src\\test\\java\\testDataTypes\\CustomerData.json";
public static void main(String args[]) throws FileNotFoundException
{
customerList=separator();
getCustomerByName("firstName");
CustomerAddress=SeparatorForAddress();
getCustomerAddress("streetAddress");
}
public static List<CustomerDataTypes> separator() throws FileNotFoundException
{
Gson gson=new Gson();
JsonReader reader=new JsonReader(new FileReader(CustomerFilePath));
CustomerDataTypes[] data=gson.fromJson(reader, CustomerDataTypes[].class);
System.out.println(Arrays.asList(data));
return Arrays.asList(data);
}
public final static CustomerDataTypes getCustomerByName(String customerName){
for(CustomerDataTypes customer : customerList) {
if(customer.firstName.equalsIgnoreCase(customerName))
System.out.println("");
System.out.println(customer.firstName);
System.out.println(customer.lastName);
//System.out.println(customer.address);
return customer;
}
return null;
}
public static List<Address> SeparatorForAddress() throws FileNotFoundException
{
Gson gson=new Gson();
JsonReader reader=new JsonReader(new FileReader(CustomerFilePath));
Address[] data=gson.fromJson(reader, Address[].class);
System.out.println(Arrays.asList(data));
return Arrays.asList(data);
}
public final static CustomerDataTypes.Address getCustomerAddress(String adr)
{
for(CustomerDataTypes.Address address : CustomerAddress )
{
System.out.println(address.streetAddress);
System.out.println(CustomerAddress);
if(address.streetAddress.equals(adr))
System.out.println("Hello");
System.out.println(address.streetAddress);
System.out.println(address.country);
System.out.println(address.postCode);
return address;
}
return null;
}
}
在上面的Main Class中,getCustomerAddress(),S.O.P(address.streetAddress)抛出空值。
以下是控制台上的输出:
[com.practice.SeleniumWebDriver.CustomerDataTypes@17579e0f, com.practice.SeleniumWebDriver.CustomerDataTypes@4d41cee]
Lakshay
Sharma
[com.practice.SeleniumWebDriver.CustomerDataTypes$Address@3712b94, com.practice.SeleniumWebDriver.CustomerDataTypes$Address@2833cc44]
null
java.lang.NullPointerException
at com.practice.SeleniumWebDriver.ReadJsonData.getCustomerAddress(ReadJsonData.java:72)
at com.practice.SeleniumWebDriver.ReadJsonData.main(ReadJsonData.java:25)
你能帮忙搞清楚吗?
答案 0 :(得分:0)
如果我修改了getCustomerByName()方法,我可以得到所有类的结果。
public final static CustomerDataTypes getCustomerByName(String customerName){
System.out.println(customerList.size());
for(int i=0;i<customerList.size();i++)
{
System.out.println("The value of i is" +i);
System.out.println(customerList.get(i).firstName);
System.out.println(customerList.get(i).lastName);
System.out.println(customerList.get(i).emailAddress);
System.out.println(customerList.get(i).address.city);
System.out.println(customerList.get(i).phoneNumber.mob);
}
return null;
}
答案 1 :(得分:0)
由于您正在从NullPointerException
方法返回null
getCustomerByName()
public final static CustomerDataTypes getCustomerByName(String customerName){
System.out.println(customerList.size());
for(int i=0;i<customerList.size();i++) {
System.out.println("The value of i is" +i);
System.out.println(customerList.get(i).firstName);
System.out.println(customerList.get(i).lastName);
System.out.println(customerList.get(i).emailAddress);
System.out.println(customerList.get(i).address.city);
System.out.println(customerList.get(i).phoneNumber.mob);
}
return null; // root cause for the Exception
}
进行如下修改,
public final static CustomerDataTypes getCustomerByName(String customerName){
System.out.println(customerList.size());
for(int i = 0; i < customerList.size(); i++) {
System.out.println("The value of i is" +i);
System.out.println(customerList.get(i).firstName);
System.out.println(customerList.get(i).lastName);
System.out.println(customerList.get(i).emailAddress);
System.out.println(customerList.get(i).address.city);
System.out.println(customerList.get(i).phoneNumber.mob);
// changes to be made
CustomerDataTypes custData = customerList.get(i);
if (customerName.equalsIgnoreCase(custData.firstName)) {
return custData;
}
}
return null;
}
如果您从null
方法返回getCustomerByName()
,如果给定名称不匹配,则应在使用该对象之前检查空安全性。
CustomerDataTypes data = getCustomerByName("Virender");
if (data != null) {
// code
}
请注意,我将实际用户名(Virender)作为参数传递给getCustomerByName()
方法而不是您在代码中使用的JSON密钥。
同样适用于getCustomerByAddress()
方法。根据您的要求进行更改。