简体   繁体   English

创建JSON对象

[英]Creating a JSON object

I am making a JSON request to server using Java. 我正在使用Java向服务器发出JSON请求。 Here is the following parameters. 以下是以下参数。

{method:'SearchBySearchConfiguration',params:[{{SearchCriteria:'%arriva',
 IsAccountSearch:true,IsContactSearch:false,SearchByName:true,SearchByAddress:false
 CRMTextValues:[], CRMCurrencyValues:[]}]}

I could do this way. 我可以这样做。

 JSONObject json=new JSONObject();
 json.put("method", "SearchBySearchConfiguration"); 

How do I add the rest of params, in name-value pair to JSON object? 如何将名称 - 值对中的其余参数添加到JSON对象?

Thanks in advance! 提前致谢!

One way I can think of is using the org.json library. 我能想到的一种方法是使用org.json库。 I wrote a sample to build part of your request object: 我写了一个示例来构建您的请求对象的一部分:

public static void main(String[] args) throws JSONException {

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("method", "SearchBySearchConfiguration");

    JSONArray jsonArray = new JSONArray();
    JSONObject innerRecord = new JSONObject();
    innerRecord.put("SearchCriteria", "%arriva");
    innerRecord.put("IsAccountSearch", true);

    jsonArray.put(innerRecord);
    jsonObject.put("params",jsonArray);

    System.out.println("jsonObject :"+jsonObject);

}

The output is : 输出是:

jsonObject :{"method":"SearchBySearchConfiguration","params":[{"IsAccountSearch":true,"SearchCriteria":"%arriva"}]}

Another technique would be to build Java objects that resemble your request structure. 另一种技术是构建类似于您的请求结构的Java对象。 You can then convert it into json using Jackson library's ObjectMapper class. 然后,您可以使用Jackson库的ObjectMapper类将其转换为json。

In both cases once you get the json string, you can directly write it into the request body. 在这两种情况下,一旦获得json字符串,就可以直接将其写入请求体。

JSONObject json=new JSONObject();
  json.put("method", "SearchBySearchConfiguration"); 
  JSONArray paramsArr = new JSONArray();
    JSONObject arrobj = new JSONOject();
      arrobj.put("SearchCriteria","%arriva");
      arrobj.put("IsAccountSearch","true");
      arrobj.put("IsContactSearch","false");
      arrobj.put("SearchByName","true");
      arrobj.put("SearchByAddress","false");
      arrobj.put("CRMTextValues",new JSONArray());
      arrobj.put("CRMCurrencyValues",new JSONArray());
    paramsArr.put(arrobj);
  json.put("params",paramsArr);

您可以创建JSONArray并将该数组放在JSONObject

Its Better to use gson for this. 最好使用gson

First you need to create classs with following members : 首先,您需要使用以下成员创建类:

public class TestClass{

    private String method;
    private ParamClass params;


    }
public class ParamClass{
    private String SearchCriteria;
    private boolean IsAccountSearch;
    private boolean IsContactSearch;
    private boolean SearchByName;
    private boolean SearchByAddress;
    private String[] CRMTextValues;
    private String[] CRMCurrencyValues;
}

Usage : 用法:

Serializing : 序列化:

Gson gson = new Gson();    
String jsonString = gson.toJson(testClassObject);

Deserializing : 反序列化:

Gson gson = new Gson();
TestClass testClassObject = gson.fromJson(jsonString , TestClass.class);

See this below example, where a JSONArray is returned and then how i am converting it in JSONObject form... 请参阅下面的示例,其中返回JSONArray,然后我将如何以JSONObject形式转换它...

public JSONArray go() throws IOException, JSONException {

JSONArray json = readJsonFromUrl("http://www.xxxxxxxx.com/AppData.aspx");

return json;
 }


JSONArray jarr;

for(int i=0 ; i<jarr.length() ; i++){

JSONObject jobj = jarr.getJSONObject(i);

String mainText = new String();
String provText = new String();
String couText = new String();      

try{

mainText = jobj.getString("Overview");
System.out.println(mainText);
}catch(Exception ex){}

try{

JSONObject jProv = jobj.getJSONObject("Provider");
provText = jProv.getString("Name");
System.out.println(provText);
}catch(Exception ex){}



try{                

JSONObject jCou = jobj.getJSONObject("Counterparty");
couText = jCou.getString("Value");
System.out.println(couText);
}catch(Exception ex){}

Jackson is a very efficient to do JSON Parsing 杰克逊是一个非常有效的JSON解析

See this link: 看到这个链接:

http://jackson.codehaus.org/ http://jackson.codehaus.org/

Gson is provided by google which is also a good way to handle JSON. Gson由google提供,这也是处理JSON的好方法。

To add params , JSONArray is used. 要添加params ,使用JSONArray Inside params, we use JSONObject to add data such as SearchByAddress, IsAccountSearch ..etc. 在params中,我们使用JSONObject添加SearchByAddress,IsAccountSearch ..etc等数据。 Reference http://www.mkyong.com/java/json-simple-example-read-and-write-json/ 参考http://www.mkyong.com/java/json-simple-example-read-and-write-json/

package com.test.json;

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonSimpleExample {
     public static void main(String[] args) {

    JSONObject obj = new JSONObject();
    obj.put("method", "SearchBySearchConfiguration");

    JSONArray list = new JSONArray();
    JSONObject innerObj = new JSONObject();

    innerObj.put("SearchCriteria","%arriva" );
    innerObj.put("IsAccountSearch",true);
    innerObj.put("IsContactSearch",false);
    innerObj.put("SearchByName",true);
    innerObj.put("SearchByAddress",false);
    innerObj.put("CRMTextValues",new JSONArray());
    innerObj.put("CRMCurrencyValues",new JSONArray());

    list.add(innerObj);

    obj.put("params", list);
    System.out.print(obj);

     }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM