JSONObject未正确转换为String

时间:2017-11-23 17:28:30

标签: java json string

我正在将JSONObject转换为String。我使用下面的代码:

String decresponse=obj.getFileWithUtil("Files/v3user22.txt");
System.out.println("Decrypted string is "+decresponse);
JSONObject js = JSONObject(decresponse);
System.out.println("JSON Object is "+js.toString());

这里,我从文件中获取decresponse的值,因为json非常大。响应的价值是:

{
    "userid":123456,
    "status":"SUCCESS",
    "name":{
        "firstName":"firstname",
        "lastName":"lastname"
    },
    "dob":"03/02/1993",
    "gender":"M",
    "kycType":"Manual",
    "address":{
        "permanentAddress":{
            "country":"INDIA",
            "street_1":"K-26",
            "street_2":"",
            "city":"North",
            "state":"Delhi",
            "postal_code":"110052",
            "locality":"abc"
        },
        "correspondenceAddress":{
            "country":"INDIA",
            "street_1":"abc",
            "street_2":"abc",
            "city":"ABC",
            "state":"Punjab",
            "postal_code":"111000",
            "locality":"def"
        }
    },
    "docs":[
        {
            "nameOnDoc":"name",
            "verificationStatus":"FAILED",
            "kycNameMatch":"SUCCESS",
            "docCode":"aadhar",
            "docValue":"1898989",
            "submittedAs":"AdditionalDoc"
        },
        {
            "nameOnDoc":"abc",
            "verificationStatus":"NOT_ATTEMPTED",
            "kycNameMatch":"NOT_ATTEMPTED",
            "docCode":"pan",
            "docValue":"KSKA1234F",
            "submittedAs":"AdditionalDoc",
            "expiryDate":"03/02/2018"
        },
        {
            "docCode":"voter",
            "docValue":"CIBPS2107P",
            "submittedAs":"Poi_Poa"
        }
    ],
    "agents":[
        {
            "bankAgentType":"BF",
            "agentBranch":"nodia",
            "agentDesignation":"agent manager",
            "agentEmpcode":"1010111",
            "custId":"119990",
            "agentId":"",
            "agencyType":"CFA",
            "agencyName":"internal"
        },
        {
            "bankAgentType":"BC",
            "agentBranch":"nodia",
            "agentDesignation":"agent manager",
            "agentEmpcode":"",
            "custId":"119999",
            "agentId":"MORPHO-1782",
            "agencyType":"VA",
            "agencyName":"morpho"
        }
    ],
    "relatives":[
        {
            "relationShip":"FATHER",
            "firstName":"firstname",
            "lastName":"lastname"
        },
        {
            "relationShip":"MOTHER",
            "firstName":"firstname",
            "lastName":"lastname"
        }
    ],
    "useKycDetails":"UNDER_REVIEW",
    "amlflags":{
        "sanction":"N",
        "pep":"N"
    },
    "walletflags":{
        "upgraded":"1",
        "updated":"1",
        "blocked":"0"
    },
    "suspended":"false",
    "aadhar_type1_check":"FAILED",
    "aadhar_kyc_name_check":"SUCCESS",
    "aadharSubmittedAs":"AdditionalDoc",
    "aadharVerified":"false",
    "panSubmittedAs":"AdditionalDoc",
    "panVerified":"false",
    "maritalStatus":"MARRIED",
    "profession":"PRIVATE_SECTOR_JOB",
    "nationality":"INDIAN",
    "kycVerificationDate":"04/01/2017",
    "declarationPlace":"Delhi",
    "dmsInfos":[
        {
            "type":"",
            "dmsid":""
        }
    ],
    "aadharAuthCode":"56bd65db0dbc4b2a848841a44eabb54e",
    "agriculturalIncome":"100000",
    "nonAgriculturalIncome":"50000",
    "seedingStatus":"consent_given"
}

但是,在将json对象转换为字符串时,值如下所示:

{
    "panVerified":"false",
    "gender":"M",
    "userid":123456,
    "panSubmittedAs":"AdditionalDoc",
    "aadharAuthCode":"56bd65db0dbc4b2a848841a44eabb54e",
    "docs":[
        {
            "kycNameMatch":"SUCCESS",
            "verificationStatus":"FAILED",
            "nameOnDoc":"name",
            "docCode":"aadhar",
            "docValue":"1898989",
            "submittedAs":"AdditionalDoc"
        },
        {
            "expiryDate":"03/02/2018",
            "kycNameMatch":"NOT_ATTEMPTED",
            "verificationStatus":"NOT_ATTEMPTED",
            "nameOnDoc":"abc",
            "docCode":"pan",
            "docValue":"KSKA1234F",
            "submittedAs":"AdditionalDoc"
        },
        {
            "docCode":"voter",
            "docValue":"CIBPS2107P",
            "submittedAs":"Poi_Poa"
        }
    ],
    "aadhar_type1_check":"FAILED",
    "aadharSubmittedAs":"AdditionalDoc",
    "useKycDetails":"UNDER_REVIEW",
    "kycVerificationDate":"04/01/2017",
    "kycType":"Manual",
    "profession":"PRIVATE_SECTOR_JOB",
    "address":{
        "permanentAddress":{
            "country":"INDIA",
            "street_1":"K-26",
            "city":"North",
            "street_2":"",
            "locality":"abc",
            "state":"Delhi",
            "postal_code":"110052"
        },
        "correspondenceAddress":{
            "country":"INDIA",
            "street_1":"abc",
            "city":"ABC",
            "street_2":"abc",
            "locality":"def",
            "state":"Punjab",
            "postal_code":"111000"
        }
    },
    "nonAgriculturalIncome":"50000",
    "seedingStatus":"consent_given",
    "dmsInfos":[
        {
            "dmsid":"",
            "type":""
        }
    ],
    "relatives":[
        {
            "firstName":"firstname",
            "lastName":"lastname",
            "relationShip":"FATHER"
        },
        {
            "firstName":"firstname",
            "lastName":"lastname",
            "relationShip":"MOTHER"
        }
    ],
    "suspended":"false",
    "agents":[
        {
            "agentId":"",
            "agentEmpcode":"1010111",
            "custId":"119990",
            "agentBranch":"nodia",
            "agentDesignation":"agent manager",
            "bankAgentType":"BF",
            "agencyType":"CFA",
            "agencyName":"internal"
        },
        {
            "agentId":"MORPHO-1782",
            "agentEmpcode":"",
            "custId":"119999",
            "agentBranch":"nodia",
            "agentDesignation":"agent manager",
            "bankAgentType":"BC",
            "agencyType":"VA",
            "agencyName":"morpho"
        }
    ],
    "amlflags":{
        "sanction":"N",
        "pep":"N"
    },
    "aadhar_kyc_name_check":"SUCCESS",
    "nationality":"INDIAN",
    "dob":"03/02/1993",
    "walletflags":{
        "upgraded":"1",
        "blocked":"0",
        "updated":"1"
    },
    "name":{
        "firstName":"firstname",
        "lastName":"lastname"
    },
    "aadharVerified":"false",
    "maritalStatus":"MARRIED",
    "status":"SUCCESS",
    "declarationPlace":"Delhi",
    "agriculturalIncome":"100000"
}

为什么我会得到不同的价值观?

1 个答案:

答案 0 :(得分:1)

  

为什么我会得到不同的值

这些值不是 不同。他们只是以不同的顺序拥有key:value对。

JSON结构包含key:value对,其中key是唯一的。在大多数情况下,键的顺序并不重要,因此像org.json.JSONObject这样的类将它们存储在内部HashMap中,这不会保留插入顺序(但允许快速访问值)。

在内部调用toString()时,它使用该HashMap迭代器构建String,因此顺序取决于键的数量及其哈希值,而不是插入顺序。

如果您想保留订单,请考虑使用其他库,例如gson。您的解析可能如下所示:

JsonParser jsonParser = new JsonParser();
JsonObject js = jsonParser.parse(decresponse).getAsJsonObject();

js.toString()会导致
{"userid":123456,"status":"SUCCESS","name":{"firstName":"firstname", ...这似乎就是你所追求的。