Java + JSON 和“errorjava.lang.RuntimeException: Stub!”错误

发布于 2024-12-22 23:46:08 字数 629 浏览 1 评论 0原文

我创建了一个 java 控制台应用程序,但我不知道为什么它返回错误“Stub!”。 这是我的代码:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class HelloWorld {
    public static void main(String[] args) {
        JSONObject jsonObject = null;
        try {
            JSONArray jsonArray = new JSONArray("{0:0}");
            jsonObject = jsonArray.getJSONObject(0);
        }
        catch (Exception e) {
            System.out.println("error" + e);
        }
    }
}

它返回我:

errorjava.lang.RuntimeException: Stub!

如何将 JSON 对象“{0:0}”更改为 java 中的对象(数组)?

I created an java console application, but I don't know why it returns me an error "Stub!".
This is my code:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class HelloWorld {
    public static void main(String[] args) {
        JSONObject jsonObject = null;
        try {
            JSONArray jsonArray = new JSONArray("{0:0}");
            jsonObject = jsonArray.getJSONObject(0);
        }
        catch (Exception e) {
            System.out.println("error" + e);
        }
    }
}

It returns me:

errorjava.lang.RuntimeException: Stub!

How to change JSON object "{0:0}" to object (array) in java?

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

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

发布评论

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

评论(1

二智少女 2024-12-29 23:46:08

杰克逊库解决了我的问题(感谢@fge)。
这是工作示例:

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

class User {
    public enum Gender { MALE, FEMALE };
    public static class Name {
      private String _first, _last;
      public String getFirst() { return _first; }
      public String getLast() { return _last; }
      public void setFirst(String s) { _first = s; }
      public void setLast(String s) { _last = s; }
    }
    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;
    public Name getName() { return _name; }
    public boolean isVerified() { return _isVerified; }
    public Gender getGender() { return _gender; }
    public byte[] getUserImage() { return _userImage; }
    public void setName(Name n) { _name = n; }
    public void setVerified(boolean b) { _isVerified = b; }
    public void setGender(Gender g) { _gender = g; }
    public void setUserImage(byte[] b) { _userImage = b; }
}

public class HelloWorld {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
        try {
            User user = mapper.readValue("{\"name\":{ \"first\" : \"Joe\", \"last\" : \"Sixpack\" }}", User.class);
            System.out.println(user.getName().getFirst());
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

它返回“Joe”

Jackson library resolved my problem (thanks @fge).
This is working example:

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

class User {
    public enum Gender { MALE, FEMALE };
    public static class Name {
      private String _first, _last;
      public String getFirst() { return _first; }
      public String getLast() { return _last; }
      public void setFirst(String s) { _first = s; }
      public void setLast(String s) { _last = s; }
    }
    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;
    public Name getName() { return _name; }
    public boolean isVerified() { return _isVerified; }
    public Gender getGender() { return _gender; }
    public byte[] getUserImage() { return _userImage; }
    public void setName(Name n) { _name = n; }
    public void setVerified(boolean b) { _isVerified = b; }
    public void setGender(Gender g) { _gender = g; }
    public void setUserImage(byte[] b) { _userImage = b; }
}

public class HelloWorld {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
        try {
            User user = mapper.readValue("{\"name\":{ \"first\" : \"Joe\", \"last\" : \"Sixpack\" }}", User.class);
            System.out.println(user.getName().getFirst());
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

it returns "Joe"

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