我试图从Json为对象提供属性。
我正在使用Jackson,而我的问题在尝试将Json属性映射到对象时是一个IO异常。
这是对象:
<body onload="Pinger_ping('google.com')">
...
<p id = "status">Checking server status...</p>
...
<script type="text/javascript">
function Pinger_ping(ip, callback) {
if(!this.inUse) {
this.inUse = true;
this.callback = callback
this.ip = ip;
var _that = this;
this.img = new Image();
var status=document.getElementById('status');
this.img.onload = function() {status.innerHTML="online";};
this.img.onerror = function() {status.innerHTML="online";};
this.start = new Date().getTime();
this.img.src = "http://" + ip;
this.timer = setTimeout(function() {status.innerHTML="offline";}, 1500);
}
}
</script>
这就是我试图将Json值赋予对象的方式。
public class TextBox {
private String label;
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
我尝试过不同种类的Json格式,并试图从文件中读取它。
有什么建议吗? 感谢
答案 0 :(得分:0)
从您的JSON中删除您的符号(&#34; \ t&#34;,&#34; \ n&#34;等)。
您还需要添加@JsonProperty(&#34;标签&#34;)(和类型)注释,并在TextBox类中创建一个空构造函数。
您的JSON字符串应该是(然后您可以格式化):
"{TextBox:[{label:Nombre, type:TextBox}]}"
但是我认为这个JSON格式不正确,为什么一个数组内部&#34; TextBox&#34;?我认为应该是:
"{TextBox:{label:Nombre, type:TextBox}}"
答案 1 :(得分:0)
问题是字段名TextBox,它在textboxTextBox类中搜索它并且没有找到它,我删除它并格式化我的JSON如下:
"{\n" +
"\t\t\t\t\"label\": \"Nombre\",\n" +
"\t\t\t\t\"type\": \"TextBox\"\n" +
"\t\t\t}";
正如dolphinziyo所说,所有的/ n和/ t都是可选的,这里是给它的logcat:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“textBox”(类com.example.ealcazar.jacksontest.TextBox),未标记为可忽略(2个已知属性:“label”,“type”] )