我想从JSON下面获取code
的值但是我收到如下错误: -
显示java.lang.NullPointerException
我在下面的代码行中收到错误
Iterator<String> iterator = companyList.iterator();
我有如下JSON对象: -
{
"products":
{
"productsApp13": {
"code": "productsApp13",
"name": "productsApp13",
"attribute_set": "Apparel",
"product_type": "product",
"status": "active"
}
}
}
我的代码: -
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
try {
Object obj1 = parser.parse(new FileReader(path.directorypath));
JSONObject jsonObject = (JSONObject) obj1;
String name = (String) jsonObject.get("products").toString();
System.out.println("Testing Parse Value = "+name);
request.payload = name;
JSONArray companyList = (JSONArray) jsonObject.get("code");
Iterator<String> iterator = companyList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
我知道productsApp13
或code
键不是数组,而我无法识别任何读取此特定值的方法。
此外,我还想知道如何修改有效负载的此值
答案 0 :(得分:0)
这个最小的例子对我有用:
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import java.io.StringReader;
public class Main {
public static void main(String[] args) {
final JsonReader jsonReader = Json.createReader(new StringReader("{\n" +
" \"products\":\n" +
" {\n" +
" \"productsApp13\": {\n" +
" \"code\": \"productsApp13\",\n" +
" \"name\": \"productsApp13\",\n" +
" \"attribute_set\": \"Apparel\",\n" +
" \"product_type\": \"product\",\n" +
" \"status\": \"active\"\n" +
" }\n" +
" }\n" +
"}"));
final JsonObject jsonObject = jsonReader.readObject();
final JsonValue products = jsonObject.get("products");
final JsonValue productsApp13 = ((JsonObject) products).get("productsApp13");
final JsonValue code = ((JsonObject) productsApp13).get("code");
System.out.println("code = " + code); // code = "productsApp13"
}
}
要访问javax.json.*
我使用Maven依赖
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
答案 1 :(得分:0)
尝试以下希望它可以解决您的问题
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.json.JSONObject;
public class Main {
public static void main(final String[] args) throws Exception {
final JSONObject jsonObject = new JSONObject(getResponseAsString(getFilePath()));
final JSONObject products = jsonObject.getJSONObject("products");
final String name = products.toString();
System.out.println("Testing Parse Value = " + name);
final JSONObject productsApp13 = products.getJSONObject("productsApp13");
final String code = productsApp13.getString("code");
System.out.println("code value : " + code);
}
private static Path getFilePath() throws URISyntaxException, FileNotFoundException {
URL url = Main.class.getClassLoader().getResource("jsonFile.txt");
if (url == null) {
throw new FileNotFoundException();
}
return Paths.get(url.toURI());
}
private static String getResponseAsString(final Path filePath) throws FileNotFoundException, IOException {
return new String(Files.readAllBytes(filePath));
}
}
上面编译的Maven依赖
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>
答案 2 :(得分:0)
代码对我有用: -
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONObject jsonObject1 = (JSONObject) jsonObject.get("products");
JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15");
String firstName = (String) jsonObject2.get("code").toString();