0

i'm getting http POST request and convert stream to string

result = convertInputStreamToString(inputStream);

convert inputstream to String

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;
    inputStream.close();
    return result;
}

im getetting result format

{
    "id":"745741798",
    "name":"Oleg",
    "pswhash":"$2a$10$rC8",
    "phone":"+380 99 444 555",
    "email":"[email protected]",
    "status":"user",
    "regdate":"2015-10-30T17:52:53.076Z"
}

How to process result to String id = "745741798"; String name = "Oleg"; etc.

1

4 Answers 4

2

You should take a look at JSON libraries that can do what you want.

Sign up to request clarification or add additional context in comments.

Comments

1

From your question, I have understood that you need to parse the values from the json.

For your case,

String jsonStr = "{
    "id":"745741798",
    "name":"Oleg",
    "pswhash":"$2a$10$rC8",
    "phone":"+380 99 444 555",
    "email":"[email protected]",
    "status":"user",
    "regdate":"2015-10-30T17:52:53.076Z"
}"

Create data model class as,

public class Data {
    String id;
    String name;
    String pswhash;
    String phone;
    String email;
    String status;
    Date regdate;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPswhash() {
        return pswhash;
    }

    public String getPhone() {
        return phone;
    }

    public String getEmail() {
        return email;
    }

    public String getStatus() {
        return status;
    }

    public Date getRegdate() {
        return regdate;
    }

    public Class(String id, String name, String pswhash, String phone, String email, String status, Date regdate) {
        this.id = id;
        this.name = name;
        this.pswhash = pswhash;
        this.phone = phone;
        this.email = email;
        this.status = status;
        this.regdate = regdate;
    }
}

Then use GSon for parsing,

Gson gson = new GSon();
Data data = gson.fromJson(jsonStr, Data.class);

After that, you can use like,

String id = data.getId();
String name = data.getName();

and so on.

Comments

0

Have a look at the jackson library: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ or gson: https://github.com/google/gson

Comments

0

This is JSON string object and you can easily convert it into class object using GSON library of google.

Create Model class of your result with getter and setter method.

public class Result
{
private String id;

private String phone;

private String status;

private String email;

private String regdate;

private String name;

private String pswhash;

public String getId ()
{
    return id;
}

public void setId (String id)
{
    this.id = id;
}

public String getPhone ()
{
    return phone;
}

public void setPhone (String phone)
{
    this.phone = phone;
}

public String getStatus ()
{
    return status;
}

public void setStatus (String status)
{
    this.status = status;
}

public String getEmail ()
{
    return email;
}

public void setEmail (String email)
{
    this.email = email;
}

public String getRegdate ()
{
    return regdate;
}

public void setRegdate (String regdate)
{
    this.regdate = regdate;
}

public String getName ()
{
    return name;
}

public void setName (String name)
{
    this.name = name;
}

public String getPswhash ()
{
    return pswhash;
}

public void setPswhash (String pswhash)
{
    this.pswhash = pswhash;
}

@Override
public String toString()
{
    return "ClassPojo [id = "+id+", phone = "+phone+", status = "+status+", email = "+email+", regdate = "+regdate+", name = "+name+", pswhash = "+pswhash+"]";
}
}

Convert your result string into Result class object like this.

Result result = gson.fromJson(resultJsonString, Result.class);
System.out.println(result.getId());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.