我想将XML String解析为JSON,我进行搜索并知道可以使用库java-json来完成此操作。我在build.gradle中添加了依赖项

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.hnib.docbaoonline"
        minSdkVersion 15
        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.0.1'
    compile 'com.android.support:design:23.0.1'
}


我做了什么 :

JSONObject jsonObj = null;
try {
    jsonObj = XML.toJSONObject(sampleXml);
} catch (JSONException e) {
    Log.e("JSON exception", e.getMessage());
    e.printStackTrace();
}


Android Studio通知我我无法使用XML类,我检查并发现我无法导入包org.json.XML

import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;  // error here


我不知道为什么,我也尝试添加jar文件lib,但结果相同。
有人有同样的问题,如何解决?

最佳答案

在应用程序/模块的libs文件夹中的.jar文件中,选择您要粘贴.jar文件的位置,在导航中选择Project视图,右键单击该.jar文件,然后选择添加为库。

要么

在您的build.gradle中,添加compile files('libs/json-20150729.jar')
依赖项块。

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

    //This :
    compile files('libs/json-20150729.jar')

}

关于android - 无法在Android Studio上使用org.json.XML类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33050846/

10-12 00:39
查看更多