How do I retrieve the eMail variable in Java from this data structure?

0

How do I recover the eMail variable in Java from this data structure? This is the structure: When making a print, it displays like this:

{
   Telefono =0, [email protected], monto=$54977, 
   Numero Documento=0, Origen de Pago:=Subsidio, 
   Tipo de Pago:=Transferencia , 
   Banco=RABOBANK CHILE, 
   Tipo de Cuenta=CUENTA VISTA, 
   Numero de Cuenta=0003331111
}
    
asked by Kakaroto 07.03.2017 в 15:46
source

2 answers

0

Try this code, it's all commented

String text = "{Telefono =0, [email protected], monto=$54977, Numero Documento=0, Origen de Pago:=Subsidio, Tipo de Pago:=Transferencia, Banco=RABOBANK CHILE, Tipo de Cuenta=CUENTA VISTA, Numero de Cuenta=0003331111}";
text = text.replace("{","").replace("}","").replace(":","");    //Limpiamos un poco quitando las llaves y los dos punto
String valorEmail = ""; //inicializamos el string donde se guardará lo que buscas
List<String> datosSeparados = Arrays.asList(text.split(",")); //hacemos un split para separar en una lista cuando encuentre una coma "," 
for(int i=0; i<datosSeparados.size(); i++){ //recorremos los datosSeparados
    List<String> valores = Arrays.asList(datosSeparados.get(i).split("=")); //hacemos un segundo split, para separar el nombre y su valor
    if(valores.get(0).toUpperCase().trim().contains("EMAIL")){  //buscamos el nombre email
        valorEmail= valores.get(1).trim();  //guardamos cuando encuentre email y quitamos espacios al rededor de las comas
    }
}

if(!valorEmail.equals("")){ //preguntamos si valorEmail es distinto al inicial
    Log.d(TAG, "Sí se encontró email, implementa acá lo que deseas hacer");
}
    
answered by 07.03.2017 в 18:37
0

At first glance your data looks like a JSON object but in reality it is not. Then, to extract the email you can resort to regular expressions, looking for anything between eMail= and , :

I have edited the question with a complete program.

DEMO

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

class ExtraerEmail
{
    public static void main (String[] args) throws java.lang.Exception
    {
    String text = "{Telefono =0,[email protected],monto=$54977,Numero Documento=0,Origen de Pago:=Subsidio,Tipo de Pago:=Transferencia,Banco=RABOBANK CHILE,Tipo de Cuenta=CUENTA VISTA,Numero de Cuenta=0003331111}";
    final Pattern pattern = Pattern.compile("eMail=(.+?),"); //buscar valor entre eMail= y ,
    final Matcher matcher = pattern.matcher(text);
    matcher.find();
    String sEmail=matcher.group(1); //Extraer cadena encontrada
    System.out.println("Este es el email: "+ sEmail); // Prints String I want to extract    


    }
}

Result

  

This is the email: [email protected]

@Deprecated

It is not a JSON object, ignore what follows:

What you submit is Json object, called in Java JSONObject . OJo a JSON object is not the same as a JSONArray.

To read the email it would be something like this:

JSONObject obj = new JSONObject(str);
String email = obj.getString("eMail");

Note: I assume that str is the variable that you print to show you the values between {} . If it's not a string, but a JSON itself, you get the email by calling .getString("eMail") on the name of your Json object directly.

See the Java Help .

    
answered by 07.03.2017 в 16:03