У меня проблемы с преобразованием объектов в JSONObject (org.json.JSONObject) в Java.
У меня есть объект X, который является дочерним объектом Y. Когда я создаю «новый JSONObject (this) .ToString ()», только атрибуты объекта X (дочернего), которые создаются в JSONObject.
См. Пример ниже:
Родительский класс:
import org.json.JSONObject;
public class Class1 {
private String name;
private int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String toString(){
return new JSONObject(this).toString();
}
}
Детский класс:
import org.json.JSONObject;
public class Class2 extends Class1 {
private String attrChildString;
private int attrChildInt;
/**
* @param args
*/
public static void main(String[] args) {
Class2 class2 = new Class2();
class2.setName("test");
class2.setNumber(5);
class2.setAttrChildInt(10);
class2.setAttrChildString("child");
System.out.println("toString child : " + class2.toString());
System.out.println("toString Parent : " + class2.toStringParent());
}
public String getAttrChildString() {
return attrChildString;
}
public void setAttrChildString(String attrChildString) {
this.attrChildString = attrChildString;
}
public int getAttrChildInt() {
return attrChildInt;
}
public void setAttrChildInt(int attrChildInt) {
this.attrChildInt = attrChildInt;
}
public String toString(){
return new JSONObject(this).toString();
}
public String toStringParent(){
return super.toString();
}
}
Результат: toString child: {"attrChildInt": 10, "attrChildString": "child"}
toString Parent: {"attrChildInt": 10, "attrChildString": "child"}
Но мне нужно отобразить значения родителя.
Пример: {"attrChildInt": 10, "attrChildString": "child", "name": "test", "number": "5"}
Есть идеи?
Спасибо.