JSONでJSON配列の文字列値を取得します。

スラジュプラサド:

下記のJSONペイロードについてI'amは、最初の配列要素を取得しようとしてEMAIL_ADDRESSをしかし、コードの下に使用して、私は、電子メールアドレスを取得しますが、配列のブラケット等を引用符で:["[email protected]"]私はメールアドレスのみのテキストを必要としています。最初の要素の配列。

ペイロード:

{
   "valid":{
      "email_addresses":[
         "[email protected]"
      ]  
   }
}

コード:

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(jsonfilepath));
JSONObject jsonObjects = (JSONObject) parser.parse(jsonObject.get("valid").toString());
String email = jsonObjects.get("email_addresses").toString();
System.out.println("Email address:"+email);
Kostakiiiis:

たぶん、このunittestのはあなたを助けることができます

   @Test
   public void test() throws JSONException, FileNotFoundException {
      JSONObject json = new JSONObject(new JSONTokener(new FileInputStream(new File(jsonfilepath))));
      JSONObject valid = (JSONObject) json.get("valid");
      Object emailAdresses = valid.get("email_addresses");
      if (emailAdresses instanceof JSONArray) {
         JSONArray emailAdressArray = (JSONArray) emailAdresses;
         Object firstEmailAdress = emailAdressArray.get(0);
         System.out.println(firstEmailAdress.toString());
      }
   }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=338976&siteId=1
おすすめ