0

我需要在我的 Android 应用程序中解析一个相当大的 json,从我在网上阅读的内容来看,Jackson 是我所需要的。

从我所见,我认为我在使用杰克逊的方式上没有错误,但是当我尝试创建对象时仍然出现问题......也许我的内存不足......或 IDK。

我的杰克逊代码:

    public void renderProducts(String url, JSONArray ja, AjaxStatus status) throws JSONException, JsonParseException, IOException {

        if(ja == null) return;

        ArrayList<Product> list = new ArrayList<Product>();

        // new code

        JsonFactory jfactory = new JsonFactory();
        JsonParser jParser = jfactory.createJsonParser(ja.toString());

        // loop until token equal to "]"
        while (jParser.nextToken() != JsonToken.END_ARRAY) {
            String fieldname = jParser.getCurrentName();
            String name = null;
            if ("name".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              name = jParser.getText(); // display mkyong
              System.out.println(name);

            }
            fieldname = jParser.getCurrentName();
            String description = null;
            if ("description".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              description = jParser.getText(); // display mkyong
              System.out.println(description);
            }

            fieldname = jParser.getCurrentName();
            String sizes [] = null;
            if ("sizes".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              sizes = jParser.getText().split("##"); // display mkyong
              System.out.println(sizes.toString());
            }

            String prices [] = null;

            fieldname = jParser.getCurrentName();
            if ("prices".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              prices = jParser.getText().split("##"); // display mkyong
              System.out.println(prices.toString());
            }
            fieldname = jParser.getCurrentName();
            String picUrl = null;
            if ("pic_url".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              picUrl = jParser.getText(); // display mkyong
              System.out.println(picUrl);
            }
            fieldname = jParser.getCurrentName();
            String bigPicURL = null;
            if ("big_pic_url".equals(fieldname)) {

              // current token is "name",
               // move to next, which is "name"'s value
              jParser.nextToken();
              bigPicURL = jParser.getText(); // display mkyong
              System.out.println(bigPicURL);
            }
            Product p = new Product(name, sizes.toString(), description, prices.toString(),picUrl,bigPicURL,"");
            list.add(p);
            p = null;
}

如果我注释掉最后 3 行(所以我不创建我的 Product 对象列表),那么代码可以工作......所以基本上它会在控制台中显示来自 json 的正确信息......但是,如果我让最后 3 行我收到以下错误:

07-07 13:48:12.514: W/AQuery(1389): reporting:java.lang.reflect.InvocationTargetException
07-07 13:48:12.514: W/AQuery(1389):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 13:48:12.514: W/AQuery(1389):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 13:48:12.514: W/AQuery(1389):     at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:200)
07-07 13:48:12.514: W/AQuery(1389):     at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:177)
07-07 13:48:12.514: W/AQuery(1389):     at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:496)
07-07 13:48:12.514: W/AQuery(1389):     at com.androidquery.callback.AbstractAjaxCallback.afterWork(AbstractAjaxCallback.java:1261)
07-07 13:48:12.514: W/AQuery(1389):     at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:986)
07-07 13:48:12.514: W/AQuery(1389):     at android.os.Handler.handleCallback(Handler.java:605)
07-07 13:48:12.514: W/AQuery(1389):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 13:48:12.514: W/AQuery(1389):     at android.os.Looper.loop(Looper.java:137)
07-07 13:48:12.514: W/AQuery(1389):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 13:48:12.514: W/AQuery(1389):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 13:48:12.514: W/AQuery(1389):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 13:48:12.514: W/AQuery(1389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 13:48:12.514: W/AQuery(1389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 13:48:12.514: W/AQuery(1389):     at dalvik.system.NativeStart.main(Native Method)
07-07 13:48:12.514: W/AQuery(1389): Caused by: java.lang.NullPointerException
07-07 13:48:12.514: W/AQuery(1389):     at com.testehan.feedme.Activity.Main.renderProducts(Main.java:322)
07-07 13:48:12.514: W/AQuery(1389):     ... 16 more

其中第 322 行是:

Product p = new Product(name, sizes.toString(), description, prices.toString(),picUrl,bigPicURL,"");

关于我如何处理这个问题的任何想法?或者我在杰克逊方法中的错误在哪里?

谢谢

更新:我使用的代码来自http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/。如果我尝试他的代码和文件,它会按预期工作..但是如果从互联网上获取 json..那么就会出现问题..

也许是因为键:值对的顺序发生了变化?

4

1 回答 1

0

I don't use Jackson, opting, instead for XStream, which has JSON support:

    Product product = new Product("Banana", "123", 23.00);
    com.thoughtworks.xstream.XStream xstream = new XStream(new com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver());
    xstream.setMode(com.thoughtworks.xstream.XStream.NO_REFERENCES);
    xstream.alias("product", Product.class);

    System.out.println(xstream.toXML(product));

The code above gives:

{"product":{"name":"Banana","id":123,"price":23.0}}

Let me know if you need further assistance.

The linked page has code to do the reverse, as well, but:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
    // crap here
    XStream xstream = new XStream(new JettisonMappedXmlDriver());
    xstream.alias("product", Product.class);
    Product product = (Product)xstream.fromXML(json);

Also note, XStream handles collections transparently, both with JSON and XML.

于 2013-07-07T17:59:26.017 回答