0

我现在无法将此 json 字符串转换为 java 对象。我已经尝试了很多方法,到目前为止我得到的错误是“线程“主”com.google.gson.JsonSyntaxException 中的异常:com.google.gson.stream.MalformedJsonException:第 1 行第 44 列的预期值”我知道问题来自这里\"errmsg\":在我制作的字符串中。或者像"Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 49" 之类的错误,如果我删除字符串的 errmsg 部分。当我尝试时,我让它运行了几次,但我得到的所有值都是空或 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 +           
           "]";
}   
}

太感谢了!

4

1 回答 1

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;
}

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

于 2013-05-25T18:26:45.703 回答