我对intellij有些问题。 我添加了Json库,但是当我运行程序时出现此错误
错误:(8,28)错误:包org.codehaus.jackson不存在
这是我的代码
package openquality.tutorialjson;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.List;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
ListView listView;
ListAdapter adapter;
List<CubetaDTO> response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.lista);
adapter= new ArrayAdapter(this,R.layout.list,R.id.textView,response);
listView.setAdapter(adapter);
class Jackson2Example {
public void main(String[] args) {
Jackson2Example obj = new Jackson2Example();
obj.run();
}
private void run() {
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
/* List<CubetaDTO> response = mapper.readValue(new File("D:\\staff.json"), List.class);
System.out.println(response);*/
// Convert JSON string to Object
String jsonInString = "[{'TagId':123,'Estado':'Disponible'}"+
"{'TagId':153,'Estado':'Muerto'}"+
"{'TagId':324,'Estado':'Disponible'}"+
"{'TagId':fdger,'Estado':'Almorzando'}"+
"{'TagId':123423,'Estado':'Disponible'}"+
"{'TagId':65784,'Estado':'En el baño'}"+
"]";
response = mapper.readValue(jsonInString, List.class);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}`
要添加json库,请单击projec结构,librarias和+ simbol并添加库
这是build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "openquality.tutorialjson"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',
{
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
答案 0 :(得分:-1)
您需要将.jar文件粘贴到WEB-INF&gt; lib文件夹中,然后转到构建路径并从此位置添加jar并再次构建和运行项目。