Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何从使用数字作为标识符的JSON数组中获取值?_Java_Json - Fatal编程技术网

在java中,如何从使用数字作为标识符的JSON数组中获取值?

在java中,如何从使用数字作为标识符的JSON数组中获取值?,java,json,Java,Json,我正在尝试解析JSON,它是来自Web of Trust API的响应。问题是API返回的JSON使用整数作为标识符,我很难得到想要的值。我使用的是org.json:json 下面是一个JSON的示例: { "google.com": { "target": "google.com", "0": [ 94, 73 ], "1": [ 94, 73 ], "2": [ 94, 73 ], "4": [ 9

我正在尝试解析JSON,它是来自Web of Trust API的响应。问题是API返回的JSON使用整数作为标识符,我很难得到想要的值。我使用的是org.json:json

下面是一个JSON的示例:

{ 
    "google.com": { 
        "target": "google.com", 
        "0": [ 94, 73 ],
        "1": [ 94, 73 ],
        "2": [ 94, 73 ],
        "4": [ 93, 66 ],
        "categories": { 
             "501": 99, 
             "301": 43 
        }
    } 
}
我正在尝试从0和4中获取值

下面是我用来解析它的Java代码:

package eclipseurlplugin.handlers;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

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


public class JsonTest {

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
      }

      public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
          JSONObject json = new JSONObject(jsonText);
          return json;
        } finally {
          is.close();
        }
      }

      public static void main(String[] args) throws IOException, JSONException {

        JSONObject json = readJsonFromUrl("http://api.mywot.com/0.4/public_link_json2?hosts=google.com/&key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        String jsonValue = json.getJSONObject("google.com").getString("0");
        System.out.println(jsonValue);      
      }
}
我试图使用下面的代码来获取值,但我得到了一个异常。有没有一个简单的方法可以做到这一点

String jsonValue = json.getJSONObject("google.com").getString("0");

谢谢。

这是因为您试图以字符串形式访问列表。因此:

jsonObj.getString("0")
将为

{"0": "94, 73"}
但不是为了

{"0": [94, 73]}
您需要使用jsonObj.getJSONObject0来获取该列表。

在{0:[1,2]}0中,标识符具有整数数组,因此只需按如下方式获取Json数组:

JSONArray msg = (JSONArray) json.getJSONObject("google.com").getJSONArray("0");

你得到的异常是什么?这些不是整数,它们是包含数字的字符串。安迪说的-在询问异常时,总是包括你得到的异常的全文。对不起,这是我第一次在这里问问题,有点新手。你不能将java.lang.String转换为org.json.JSONArray.Oh。。抱歉jsonObject.getJSONArray0@Andy Turner