JSONObject returns 用字符串实例化后 "null" 的非空值

JSONObject returns a non null value of "null" after instantiating with string

我需要下载JSON然后存储在JSON对象中。

我正在使用 org.json.JSONArray。

所有代码都集中在一个地方:

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test
{
    public static JSONObject getJSON()
    {
    try
    {
        URL uri = new URL("http://events.makeable.dk/api/getEvents");
        HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
        if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
        {
            return null;
        }

        InputStream inputStream = urlConnection.getInputStream();

        if (inputStream != null)
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer buffer = new StringBuffer();

            try
            {
                String line;
                while ((line = br.readLine()) != null)
                {
                    buffer.append(line);
                }
                br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }



            String json = buffer.toString();

            JSONObject jObject = null;
            try {
                jObject = new JSONObject(json);
            }
            catch (JSONException e) {
                e.printStackTrace();

            }
            int i = 1;
            return jObject;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
}

测试方法

 @Test
    public void addition_isCorrect() throws Exception
    {
        JSONObject json = Test.getJSON();
        assertNotNull(json);
        assertTrue(json.length()>0);
    }

第一个断言通过,第二个不通过,导致长度 == 0。

而我得到的是 this。值为字符串 "null".

的 JSONObject 对象

没有抛出异常。我将缓冲区的内容写入文件并对其进行了验证,并且验证正常。

再拍一张http://i.imgur.com/P03MiEZ.png

为什么会这样?

Gradle

apply plugin: 'com.android.application'

android {

testOptions {
    unitTests.returnDefaultValues = true
}
compileSdkVersion 23
buildToolsVersion "24.0.0 rc3"

defaultConfig {
    applicationId "baaa.myapplication"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.+'
}

亲您先尝试获取only json = buffer.toString(); 告诉我 json.

的值

如果您的字符串 json 为空,那么文件读取代码就会出现问题。

首先将 json 字符串转换为 JSONArray,然后使用该 JSONArray 对象获取每个 JSONObject

请试一试

String json = buffer.toString();
JSONObject jsonObject = new JSONObject(json);
JsonObject updatedJson = jsonObject.optJSONObject("json");
return updatedJson;

由于您是 运行使用 Junit 编写代码,因此它 运行 安装在您计算机上的 SDK 上。此 SDK 并未提供所有内容,有些只是一些框架 class,提供方法和文档的签名,但不提供代码。所以不能直接执行。

您需要导入库才能运行 测试。

testCompile 'org.json:json:the_correct_version'

在此处查看版本:

http://mvnrepository.com/artifact/org.json/json

这是基于这个 post 的答案: