java - json 提取/解码问题 - json 中的 json?
非常简单的java/json问题。
我有以下测试代码块。我可以通过索引或键使用“.get()”获取第一个元素。但我无法通过按键获取任何其他元素...
测试失败,命令行上没有任何内容..我假设这是由于我的环境中未正确设置某些内容来显示错误结果..
更新:: 好吧..看来真正的问题是我不知道如何获取一个项目,并且首先确定它应该转换为什么“类型”。对于“昵称”,“名称”..如果我将它们转换为字符串..我会得到正确的结果..
那么,如何迭代json的键/值列表来确定如何正确获取每一项? ?
测试代码是:
import org.json.simple.JSONObject;
import org.json.simple.*;
//import org.json.simple.JSONValue;
public class asuH {
public static void main(String[] args){
final String[] arguments = args;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
String json_=arguments[0];
//--get the page for the 1st and 2nd urls...
//test the json input..
System.out.println("asdfsfd \n");
System.out.println(json_);
//JSONObject obj=new JSONObject();
//Object obj=JSONValue.parse(json_);
String k9="{\"nickname\":null,\"num\":100,\"contact\":{\"phone\":\"123456\",\"zip\":\"7890\"},\"balance\":1000.21,\"is_vip\":true,\"name\":\"foo\"}";
//JSONObject obj = (JSONObject)JSONValue.parse(json_);
JSONObject obj = (JSONObject)JSONValue.parse(k9);
System.out.print("11 \n");
String fa = (String)obj.get("nickname");
System.out.print(fa);
System.out.print("22 \n");
fa = (String)obj.get("contact"); //<< not working!!!
System.out.println("22 cc\n");
System.out.println(fa);
String ttt=obj.toString();
System.out.print(ttt);
System.out.println("\n s4354455 \n");
System.exit(0);
}
catch (Exception ex) {}
System.exit(0);
}
});
}
}
任何想法/指针都值得赞赏。
谢谢
very simple java/json question.
I have the following test chunk of code. I can get the 1st element using the ".get()" by either index, or by the key. but I can't get any other elements by key...
The test dies, with nothing on the cmdline.. I'm assuming this is due to something not being correctly set within my env to display err results..
UPDATE::
OK.. it appears that the real issue is I don't know how to get an item, and to 1st determine what "type" it should be cast to. for the "nickname","name".. if I cast them as String.. I get the correct result..
So, how can one iterate through the key/value list of the json to determime how to correctly get each item??
The test code is:
import org.json.simple.JSONObject;
import org.json.simple.*;
//import org.json.simple.JSONValue;
public class asuH {
public static void main(String[] args){
final String[] arguments = args;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
String json_=arguments[0];
//--get the page for the 1st and 2nd urls...
//test the json input..
System.out.println("asdfsfd \n");
System.out.println(json_);
//JSONObject obj=new JSONObject();
//Object obj=JSONValue.parse(json_);
String k9="{\"nickname\":null,\"num\":100,\"contact\":{\"phone\":\"123456\",\"zip\":\"7890\"},\"balance\":1000.21,\"is_vip\":true,\"name\":\"foo\"}";
//JSONObject obj = (JSONObject)JSONValue.parse(json_);
JSONObject obj = (JSONObject)JSONValue.parse(k9);
System.out.print("11 \n");
String fa = (String)obj.get("nickname");
System.out.print(fa);
System.out.print("22 \n");
fa = (String)obj.get("contact"); //<< not working!!!
System.out.println("22 cc\n");
System.out.println(fa);
String ttt=obj.toString();
System.out.print(ttt);
System.out.println("\n s4354455 \n");
System.exit(0);
}
catch (Exception ex) {}
System.exit(0);
}
});
}
}
any thoughts/pointers are appreciated.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与名为
contact
的属性对应的值不是String
。使用适当的 getter 方法,并且不要强制转换。The value corresponding to property named
contact
is not aString
. Use the appropriate getter method, and don't cast.您可以从对象中获取字段并询问其类型:
您得到图片...
You can get field from your object and ask for it's type:
You get the picture...