java - json 提取/解码问题 - json 中的 json?

发布于 2024-12-09 06:47:48 字数 1931 浏览 1 评论 0原文

非常简单的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-12-16 06:47:48

与名为 contact 的属性对应的值不是 String。使用适当的 getter 方法,并且不要强制转换。

// snip...
String fa = obj.getString("nickname");
// snip...
JsonObject contact = obj.getObject("contact");
// and so on

The value corresponding to property named contact is not a String. Use the appropriate getter method, and don't cast.

// snip...
String fa = obj.getString("nickname");
// snip...
JsonObject contact = obj.getObject("contact");
// and so on
酒儿 2024-12-16 06:47:48

您可以从对象中获取字段并询问其类型:

Object field = obj.get("field");
if (field instanceof JSONArray) {
    ...
} else if (field instanceof JSONObject) {
    ...
} else if (field instanceof Number) {
    ...
} else  {
    ...
}

您得到图片...

You can get field from your object and ask for it's type:

Object field = obj.get("field");
if (field instanceof JSONArray) {
    ...
} else if (field instanceof JSONObject) {
    ...
} else if (field instanceof Number) {
    ...
} else  {
    ...
}

You get the picture...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文