Java String字符串转Json

公司接了一个常规短信接口,接口返回的数据是String字符串,解析方法不能使用常规方式,一般的接口返回的Json和Xml数据。

result=0&faillist=&balance=18&linkid=4F7DDE3557374894B7A2B056D5CD1FEE&description=发送短信成功

使用替换的方式替换成Json,然后用阿里的包。

下面直接贴代码

package com.java.learn.demo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class Json {
    public static void main(String[] args) {
        String content ="result=0&faillist=&balance=18&linkid=4F7DDE3557374894B7A2B056D5CD1FEE&description=发送短信成功";
        content = content.replace("=","\":\"");
        System.out.println(content);
        content = content.replace("&","\",\"");
        System.out.println(content);
        content = "{\"" + content +"\"}";
        System.out.println(content);
        JSONObject object = JSON.parseObject(content);
        System.out.println(object.toJSONString());
    }
}
result":"0&faillist":"&balance":"18&linkid":"4F7DDE3557374894B7A2B056D5CD1FEE&description":"发送短信成功
result":"0","faillist":"","balance":"18","linkid":"4F7DDE3557374894B7A2B056D5CD1FEE","description":"发送短信成功
{"result":"0","faillist":"","balance":"18","linkid":"4F7DDE3557374894B7A2B056D5CD1FEE","description":"发送短信成功"}
{"result":"0","linkid":"4F7DDE3557374894B7A2B056D5CD1FEE","balance":"18","faillist":"","description":"发送短信成功"}

 

你可能感兴趣的:(java,idea)