Renjith
Renjith

Reputation: 3617

Creating a JSON object

I am making a JSON request to server using Java. 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?

Thanks in advance!

Upvotes: 1

Views: 286

Answers (6)

swemon
swemon

Reputation: 5946

To add params, JSONArray is used. Inside params, we use JSONObject to add data such as SearchByAddress, IsAccountSearch ..etc. Reference 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);

     }

}

Upvotes: 0

RobinV
RobinV

Reputation: 21

One way I can think of is using the org.json library. 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. You can then convert it into json using Jackson library's ObjectMapper class.

In both cases once you get the json string, you can directly write it into the request body.

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

See this below example, where a JSONArray is returned and then how i am converting it in JSONObject form...

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

See this link:

http://jackson.codehaus.org/

Gson is provided by google which is also a good way to handle JSON.

Upvotes: 0

Eldhose M Babu
Eldhose M Babu

Reputation: 14530

Its Better to use gson for this.

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

Upvotes: 0

V.J.
V.J.

Reputation: 9590

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

Upvotes: 1

user1646438
user1646438

Reputation:

The you can create JSONArray and put that array in JSONObject

Upvotes: 0

Related Questions