如何使用小程序转换JSON数据?

发布于 2024-12-26 01:27:01 字数 1416 浏览 5 评论 0原文

我需要在我的小程序中转换 JSON 数据。我正在使用签名的小程序。我已经签署了 gson-1.4.jar 和 java 类文件,但无法转换 JSON 数据。请给我提供样品建议。

代码:--

import com.google.gson.Gson;

import java.applet.*;

  public class MyApplet extends Applet implements Runnable {

        private Gson JSON;

        public void init() {
         JSON = new Gson();
        }

        public void start()
        {
        String json =  "{\"menu\": {\"id\": \"1\", \"value\": \"test\"} }";
        Gson gson = new Gson();
        System.out.println("start");  
        MenuWrapper m = gson.fromJson(json, MenuWrapper.class);
        System.out.println(m.getMenu().getId());
        System.out.println(m.getMenu().getValue());
        System.out.println("end");  
        }

        public void run(){
        }
}

class Menu {

    String id;
    String value;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String toString() {
        return String.format("id: %s, value: %d", id, value);
    }
}

class MenuWrapper {

    Menu menu;
    public Menu getMenu() { 
        return menu; 
    }
    public void setMenu(Menu m) { 
        menu = m; 
    }
}

未发现错误,输出为“start”

请告诉我如何解决此问题。

I need to convert JSON data in my applet program. Am using a signed applet. I have signed gson-1.4.jar and java class files, but unable to convert the JSON data. Kindly advise me with samples.

Code:--

import com.google.gson.Gson;

import java.applet.*;

  public class MyApplet extends Applet implements Runnable {

        private Gson JSON;

        public void init() {
         JSON = new Gson();
        }

        public void start()
        {
        String json =  "{\"menu\": {\"id\": \"1\", \"value\": \"test\"} }";
        Gson gson = new Gson();
        System.out.println("start");  
        MenuWrapper m = gson.fromJson(json, MenuWrapper.class);
        System.out.println(m.getMenu().getId());
        System.out.println(m.getMenu().getValue());
        System.out.println("end");  
        }

        public void run(){
        }
}

class Menu {

    String id;
    String value;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String toString() {
        return String.format("id: %s, value: %d", id, value);
    }
}

class MenuWrapper {

    Menu menu;
    public Menu getMenu() { 
        return menu; 
    }
    public void setMenu(Menu m) { 
        menu = m; 
    }
}

No error found, output comes "start"

Kindly advise me how to solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小情绪 2025-01-02 01:27:01

我会把“签名罐子”的事情放在一边。事实上,我会把整个 Applet 的事情放在一边,专注于 JSON 转换的事情。因此,首先尝试运行代码,将 JSON 实际转换为 MenuWrapper 类。运行它,您将看到以下内容之一:

  • MenuWrapper 已按预期创建,并且一切正常
    在这种情况下,配置应该有问题(您应该在java控制台中观察到异常,或者在这种情况下)
  • 在转换过程中抛出某种异常。
    在这种情况下,请在这里发布,它不会与Applet相关,而是关于JSON和使用GSON的问题。

希望这有帮助

I would put aside the "signed jars" thing. In fact, I would put aside the whole Applet thing, and concentrate on you JSON conversion stuff. So, first of all try to run your code that makes an actual conversion of your JSON into MenuWrapper class. Run it and you'll see one of the following :

  • the MenuWrapper was created as expected, and everything is ok
    In this case it should be something wrong with configuration (you should observe exception in java console or in this case)
  • some kind of Exception is thrown during the conversion.
    In this case, please post it here, it won't be related to Applets but a question about JSON and using a GSON.

Hope this helps

染柒℉ 2025-01-02 01:27:01

这里有类似的问题:

Java:在Applet中使用Gson会导致SecurityException< /a>

看来必须对小程序进行签名才能使反序列化库中的反射起作用。

There are similar questions here:

Java: Using Gson in an Applet causes SecurityException

It seems that the applet must be signed in order for the reflection in the deserialize library to work.

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