我的旧应用

时间:2016-02-04 23:35:25

标签: android json

我编写了一个从WebService检索数据并在ListView中显示这些数据的应用程序。

这是我的JSON对象:

{
    "products": [{
        "id": "240",
        "title": "Przemys\u0142 stoczniowy. Co dalej?",
        "introtext": "<p>Przemys\u0142 stoczniowy prze\u017cywa obecn[....]"
    }, {
        "id": "239",
        "title": "Ceny Franka \u2013 prognozy",
        "introtext": "</p><p>Po ostatnich zmianach w kursie Franka wiele [...]"
    }, {
        "id": "238",
        "title": "Otwarty Fundusz Emerytalny",
        "introtext": "</p><p>Saldo zakup\u00f3w i sprzeda\u017cy akcji [...]"
    }, {
        "id": "233",
        "title": "Czy waluta Bitcoin doczeka\u0142a si\u0119 gro\u017anego konkurenta?",
        "introtext": "</p><p>Bitcoin - kryptowaluta, wprowadzona w 2009 roku. [...]"
    }],
    "success": 1
}

这些是我的静态变量 - JSON节点名称:

public static final String TAG_SUCCESS = "success";
public static final String TAG_PRODUCTS = "products";
public static final String TAG_PID = "id";
public static final String TAG_NAME = "title";
public static final String TAG_INTROTEXT = "introtext";

这些工作正常。

现在我有不同的JSON:

{"status":200,
"data":
{"slide":[
{"image":"http:\/\/www.domain.com\/image\/slide\/1.jpg","link":"http:\/\/www.domain.com\/product?id=52\x0078lang=pl"},
{"image":"http:\/\/www.domain.com\/image\/slide\/2.jpg","link":"http:\/\/www.domain.com\/product?id=53\x0078lang=pl"},
{"image":"http:\/\/www.domain.com\/image\/slide\/3.jpg","link":"http:\/\/www.domain.com\/product?id=4\x0078lang=pl"},
{"image":"http:\/\/www.domain.com\/image\/slide\/4.jpg","link":"http:\/\/www.domain.com\/product?id=13\x0078lang=pl"}],
"portfolio":[
{"id":52,"name":"Aplikacja pierwsza","description":"To jest opis aplikacji pierwszej[...]"},
{"id":37,"name":"Aplikacja druga","description":"To jest opis aplikacji drugiej [...]"},
{"id":4,"name":"Aplikacja trzecia","description":"To jest opis aplikacji trzeciej [...]"},
{"id":51,"name":"Aplikacja czwarta","description":"To jest opis aplikacji czwartej [...]"},
"person":[
{"name":"Jan Kowalski","position":"Prezes","image":"http:\/\/www.domain.com\/image\/persons\/1.png"},
{"name":"Jan Nowak","position":"Dyrektor","image":"http:\/\/www.domain.com\/image\/persons\/2.png"},
{"name":"Adam Malysz","position":"Vice Director","image":"http:\/\/www.domain.com\/image\/persons\/3.png"}],
"work":[
{"name":"Programista","image":"http:\/\/www.domain.com\/image\/file-type\/android.png","file":"http:\/\/www.domain.com\/file\/ogloszenie_1.pdf"},
{"name":"Praktykant","image":"http:\/\/www.domain.com\/image\/file-type\/smarttv.png","file":"http:\/\/www.domain.com\/file\/ogloszenie_2.pdf"},
{"name":"Project Manager","image":"http:\/\/www.domain.com\/image\/file-type\/pm.png","file":"http:\/\/www.domain.com\/file\/dogloszenie_3.pdf"},
{"name":"Tester","image":"http:\/\/www.domain.com\/image\/file-type\/tester.png","file":"http:\/\/www.domain.com\/file\/ogloszenie_4.pdf"}
]}}

现在我必须提取变量:id,name和description。

当我在TAG_PRODUCTS字符串“portfolio”中写字时,我得到:

W/System.err: org.json.JSONException: No value for portfolio

当我用TAG_PRODUCTS字符串写入“数据”时 - 或者更广泛地说,我得到:

W/System.err: org.json.JSONException: Value {"work":[{"image":"http:\/\/www.domain.com\/image\/slide\/1.jpg"[...]

系统在Log中为我提供了长JSON字符串,但我的应用程序无法正常工作(最终在try-catch块中运行)。

2 个答案:

答案 0 :(得分:0)

未关闭作为porfolio键值的数组。在解析之前,您必须确保JSON格式正确。

尝试在代码中使用try - catchJSONException,在开始解析 JSON 的最开始。

答案 1 :(得分:0)

好吧,谢谢! 这些解决了我的问题:

products = json.getJSONObject("data").getJSONArray("portfolio");

data id object-container和portfolio是Sub-array