Json在Json对象或String中的完整响应

时间:2017-11-04 12:35:11

标签: java json

嗨我正在使用okhttp从服务器获取数据,而我正在通过response.body().string()获得正在打印完整数据的响应。但是,当我将响应转换为jsonobject或字符串(存储在变量中)并打印数据时,它没有显示响应的完整数据。我无法显示

"id": 1,
"user_id": 1,
"first_name": "hhhh",
"last_name": "Thakur",

而其他人工作正常 我的json数据格式为

{
"customers": [
    {
        "id": 1,
        "user_id": 1,
        "first_name": "hhhh",
        "last_name": "Thakur",
        "website": "",
        "status": 0,
        "created": "2017-10-10T07:29:45+00:00",
        "customer_info": [
            {
                "id": 1,
                "customer_id": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ],
        "customer_address": [
            {
                "id": 1,
                "customer_id": 1,
                "name": "hhhh Thakur",
                "address": "V.PO Chadwal Distt Kathua, Teh Hiranagar  Jammu, Jammu and Kashmir in",
                "city": "Shimla",
                "state": "Himachal Pradesh",
                "zip": "171004",
                "country": "India",
                "fax": "06723",
                "type": 1,
                "as_ship": 1,
                "created": "2017-10-10T07:29:45+00:00"
            },
            {
                "id": 2,
                "customer_id": 1,
                "name": "Neha Sharma",
                "address": "V.PO Chadwal Distt Kathua, Teh Hiranagar  Jammu, Jammu and Kashmir in",
                "city": "India",
                "state": "Jammu and Kashmir",
                "zip": "180012",
                "country": "India",
                "fax": "664984",
                "type": 2,
                "as_ship": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ]
    },
    {
        "id": 2,
        "user_id": 1,
        "first_name": "arun",
        "last_name": "kumar",
        "website": "www.isitwa.com",
        "status": 0,
        "created": "2017-10-11T10:14:38+00:00",
        "customer_info": [
            {
                "id": 2,
                "customer_id": 2,
                "created": "2017-10-11T10:14:38+00:00"
            }
        ],
        "customer_address": [
            {
                "id": 3,
                "customer_id": 2,
                "name": "",
                "address": "",
                "city": "",
                "state": "",
                "zip": "",
                "country": "",
                "fax": "",
                "type": 1,
                "as_ship": 1,
                "created": "2017-10-11T10:14:38+00:00"
            },
            {
                "id": 4,
                "customer_id": 2,
                "name": "",
                "address": "",
                "city": "",
                "state": "",
                "zip": "",
                "country": "",
                "fax": "",
                "type": 2,
                "as_ship": 1,
                "created": "2017-10-11T10:14:38+00:00"
            }
        ]
    }

]}

}

2 个答案:

答案 0 :(得分:0)

那是因为你的JSON无效......在这里粘贴你的JSON代码,https://codebeautify.org/jsonviewer,它将允许你操纵格式化,并更好地研究它......

我不是Java方面的专家,但列出嵌套数组(无论是JSON还是普通数组)都可以通过两种方式完成:

foreach ( $array as $item) {

    foreach ( $item as $key => $data) {
        echo $data;
    }

}

for ( $i=0; $i < $array.lenght(); $i++ ) {

    for ( $j=0; $j < $array.lenght-1; $j++) {
        print_r $array[$i][$j];
    }
}

如果你不知道你的数组是多久,或者嵌套数组,那么使用foreach方法会更好......

答案 1 :(得分:-1)

您的json数据格式不正确,请尝试在下面打印:

{
"a": [
    {
        "id": 1,
        "user_id": 1,
        "first_name": "John",
        "last_name": "",
        "b": [
            {
                "id": 1,
                "customer_id": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ],
        "c": [
            {
                "id": 1,
                "customer_id": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ]
    }],
    "b":[{
        "id": 1,
        "user_id": 1,
        "first_name": "John",
        "last_name": "",
        "b": [
            {
                "id": 1,
                "customer_id": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ],
        "c": [
            {
                "id": 1,
                "customer_id": 1,
                "created": "2017-10-10T07:29:45+00:00"
            }
        ]
    }
]}