如何通过Gson将以下JSON字符串转换为Java Object?

时间:2013-05-25 18:15:54

标签: java android database json gson

我现在遇到将此json字符串转换为java对象的问题。我已经尝试了很多方法,到目前为止我得到的错误如“线程中的异常”主“com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:第1行第44列的预期值“我知道问题来自于此处的 \”errmsg \“: 。或者像“预期BEGIN_OBJECT但在第1行第49列是BEGIN_ARRAY”之类的错误,如果我删除了我的字符串的errmsg部分。在我尝试的时候,我让它运行了几次,但我得到的所有值都是null或0,它甚至没有进入类产品。

另外一个问题:如何将网站链接写入字符串,我总是在这部分出错,这就是为什么我在我创建的字符串中注释掉这些链接的原因。

谢谢!

这是我的json:

{"data":{"results":4,"returned":1,"errmsg":""},"products":
       [{"name":"aaa", "region":"USA > CA","price":"1,231.00","year":"2011",
       "link":"http:\/\/www.aaa.com\/abc\/sss-ttt-2011\/",
       "image":"http:\/\/pic.aaa.com\/media\/8\/aaa_12_abc.jpeg"}, 
       {"name":"bbb","region":"USA > WA","price":"31.00","year":"2012",
       "link":"http:\/\/www.bbb.com\/abc\/sss-ttt-2012\/",
       "image":"http:\/\/pic.bbb.com\/media\/7\/bbb_12_abc.jpeg"}]}

这就是我所做的:

import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class product {
static String jsonInput = "{" + 
          "\"data\":" + 
          "{\"results\":4,\"returned\":2,\"errmsg\":}," + 
          "\"products\":" +
          "[" +
            "{\"name\":\"aaa\",\"region\":\"USA > CA\",\"price\":1,231.00,\"year\":2011,"+
            //\"link\":\"http:\/\/www.aaa.com\/abc\/sss-ttt-2011\/\", +
            //\"image\":\"http:\/\/pic.aaa.com\/media\/8\/aaa_12_abc.jpeg\" +
            "{\"name\":\"bbb\",\"region\":\"USA > WA\",\"price\":31.00,\"year\":2012,"+
            //\"link\":\"http:\/\/www.bbb.com\/abc\/sss-ttt-2012\/\", +
            //\"image\":\"http:\/\/pic.bbb.com\/media\/8\/bbb_12_abc.jpeg\" +
             "}"+
          "]" +     
         "}";

public static void main(String[] args) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    Data data = gson.fromJson(jsonInput, Data.class);
    System.out.println(data);
}
}

class Data {

private int results;
private int returned;
private String errmsg;

private Products products;

@Override
public String toString() {
    return String.format(
            "[data: results=%1$d, returned=%2$d, errmsg=%3$s, products=%4$s]", 
            results, returned, errmsg, products);
}
}

class Products {
private Products_info[] products_info;

@Override
public String toString() {
return String.format("[%1$s]", Arrays.toString(products_info));
}
}

class Products_info {

private String name;
private String region;
private double price;
private int year;
//private String link;
//private String image;

@Override
public String toString() {

    return "[name=" + name + ", region=" + region + ", price=" + price +
           ", year=" + year +
           //", link=" + link +
           //", image=" + image +           
           "]";
}   
}

非常感谢你!

1 个答案:

答案 0 :(得分:0)

您的String可能格式不正确,但据我所知,您定义的java对象根本不会转换为JSON数据。

这个JSON:

{
    "data": {
        "results": 4,
        "returned": 1,
        "errmsg": ""
    },
    "products": [
        {
            "name": "aaa",
            "region": "USA > CA",
            "price": "1,231.00",
            "year": "2011",
            "link": "http://www.aaa.com/abc/sss-ttt-2011/",
            "image": "http://pic.aaa.com/media/8/aaa_12_abc.jpeg"
        },
        {
            "name": "bbb",
            "region": "USA > WA",
            "price": "31.00",
            "year": "2012",
            "link": "http://www.bbb.com/abc/sss-ttt-2012/",
            "image": "http://pic.bbb.com/media/7/bbb_12_abc.jpeg"
        }
    ]
}

应翻译成以下对象:

SomeObject {
    public class Data {
        private int results;
        private int returned;
        private String errmsg;
    }

    public class Product {
        private String name;
        private String regsion;
        private String price;
        private String year;
        private String link;
        private String image;
    }

    private Data data;
    private List<Product> products;
}

值得注意的是,有许多在线格式化程序可以正确地转义字符串,因此您不必手动执行: