从字符串获取数据到另一个字符串

时间:2015-10-30 18:50:48

标签: java android json string

我正在获取http POST请求并将流转换为字符串

result = convertInputStreamToString(inputStream);

将inputstream转换为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结果格式

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

如何处理结果 String id = "745741798"; String name = "Oleg";

4 个答案:

答案 0 :(得分:2)

你应该看一下可以做你想做的JSON库。

答案 1 :(得分:1)

从你的问题来看,我已经明白你需要解析json中的值。

对于您的情况,

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

将数据模型类创建为,

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;
    }
}

然后使用GSon进行解析,

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

之后,您可以使用like,

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

等等。

答案 2 :(得分:0)

答案 3 :(得分:0)

这是JSON字符串对象,您可以使用谷歌的GSON库轻松将其转换为类对象。

使用getter和setter方法创建结果的Model类。

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+"]";
}
}

将结果字符串转换为Result类对象。

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